Sign in to follow this  
Followers 0
lucius100

How to stop loop ?

19 posts in this topic

Test loop in notepad, and i can't stop the loop

 

#include <ButtonConstants.au3>

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Test", 146, 66, 268, 198)
$Checkbox1 = GUICtrlCreateCheckbox("Test", 32, 8, 97, 17)
$Button1 = GUICtrlCreateButton("Write", 32, 32, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
 
$App = "Untitled - Notepad"
WinWait($App)
 
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
_Write()
 
EndSwitch
 WEnd
 
Func _Write()
   While 1
   ControlSend($App, "", "Edit1", "{5}")
   Sleep(5000)
   WEnd
EndFunc
 

 

Share this post


Link to post
Share on other sites



You can use a hotkey as shown below. Press F2 to get out of the loop and return to the main program.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $g_bGetOut = False ; Declare a global Flag.
HotKeySet("{F2}","GetOut") ; Set a hotkey to call a function.

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Test", 146, 66, 268, 198)
Global $Checkbox1 = GUICtrlCreateCheckbox("Test", 32, 8, 97, 17)
Global $Button1 = GUICtrlCreateButton("Write", 32, 32, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $App = "Untitled - Notepad"
WinWait($App)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Write()

    EndSwitch
WEnd

Func _Write()
    While 1
        ControlSend($App, "", "Edit1", "{5}")
        Sleep(5000)
        If $g_bGetOut Then ExitLoop ; Check if the flag was altered.
    WEnd
    $g_bGetOut = False ; Reset the flag in case you need to get out again later.
EndFunc   ;==>_Write

Func GetOut()
    $g_bGetOut = True ; Set the trigger.
EndFunc

Share this post


Link to post
Share on other sites

 

You can use a hotkey as shown below. Press F2 to get out of the loop and return to the main program.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $g_bGetOut = False ; Declare a global Flag.
HotKeySet("{F2}","GetOut") ; Set a hotkey to call a function.

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Test", 146, 66, 268, 198)
Global $Checkbox1 = GUICtrlCreateCheckbox("Test", 32, 8, 97, 17)
Global $Button1 = GUICtrlCreateButton("Write", 32, 32, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $App = "Untitled - Notepad"
WinWait($App)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Write()

    EndSwitch
WEnd

Func _Write()
    While 1
        ControlSend($App, "", "Edit1", "{5}")
        Sleep(5000)
        If $g_bGetOut Then ExitLoop ; Check if the flag was altered.
    WEnd
    $g_bGetOut = False ; Reset the flag in case you need to get out again later.
EndFunc   ;==>_Write

Func GetOut()
    $g_bGetOut = True ; Set the trigger.
EndFunc

is it possible to stop the loop with the same button?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Not really without using a toggle and an event method... however, interesting enough (almost too much lol)... that is not working for this user at the moment either.

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Not really without using a toggle and an event method... however, interesting enough (almost too much lol)... that is not working for this user at the moment either.

 

k, how about to password protect file?

i found >here , i tried the script 

Global $Password="password";put your password here...
$pass = InputBox( "Password", "Please enter password", "", "*", 190, 115)
If @Error Then Exit
If $pass = $Password Then
MsgBox( 1, "Accepted", "Password accepted!", 30)
Else
    MsgBox( 1, "Rejected!", "Incorrect Password", 5)
EndIf

;Rest of script goes here....

but the box is messy and where do i set the password for example 12345 ?

Share this post


Link to post
Share on other sites

You may want to look at the autoit wiki tutorials.  This isn't really a step by step hold your hand while you learn type of thing.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

There's a good tutorial about interrupting running functions here: https://www.autoitscript.com/wiki/Interrupting_a_running_function

This isn't the neatest code I've ever written but it allows you to get out of the loop with either a hotkey or by clicking the same button again

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $bGetOut = False ; Declare a global Flag.
Global $gb_Interrupt = 0 ; As above
HotKeySet("{F2}","GetOut") ; Set a hotkey to call a function.

Global $Form1 = GUICreate("Test", 146, 66, 268, 198)
Global $Checkbox1 = GUICtrlCreateCheckbox("Test", 32, 8, 97, 17)
Global $Button1 = GUICtrlCreateButton("Write", 32, 32, 75, 25)

GUISetState(@SW_SHOW)

 ; Intercept Windows command messages with out own handler
 GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

Global $App = "Untitled - Notepad"
WinWait($App)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            If $gb_Interrupt > 0 Then
                $gb_Interrupt = 0
            Else
                GUICtrlSetData($Button1, "Stop")
                _Write()
                GUICtrlSetData($Button1, "Write")
            EndIf
    EndSwitch
WEnd

Func _Write()
    $gb_Interrupt = 1
    While 1
        ControlSend($App, "", "Edit1", "{5}")
        Sleep(2000)
        If $bGetOut Or $gb_Interrupt = 2 Then ExitLoop ; Check if the flag was altered.
    WEnd

    $bGetOut = False ; Reset the flag in case you need to get out again later.
EndFunc   ;==>_Write

Func GetOut()
    $bGetOut = True ; Set the trigger.
EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
     #forceref $hWnd, $Msg, $lParam
     If BitAND($wParam, 0x0000FFFF) = $Button1 And $gb_Interrupt = 1 Then $gb_Interrupt = 2
     Return "GUI_RUNDEFMSG" ; $GUI_RUNDEFMSG
 EndFunc   ;==>_WM_COMMAND

;

I haven't got all my code snippets available to me right now. There should be a more elegant solution.

Edited by czardas

Share this post


Link to post
Share on other sites

There's a good tutorial about interrupting running functions here: https://www.autoitscript.com/wiki/Interrupting_a_running_function

This isn't the neatest code I've ever written but it allows you to get out of the loop with either a hotkey or by clicking the same button again

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $bGetOut = False ; Declare a global Flag.
Global $gb_Interrupt = 0 ; As above
HotKeySet("{F2}","GetOut") ; Set a hotkey to call a function.

Global $Form1 = GUICreate("Test", 146, 66, 268, 198)
Global $Checkbox1 = GUICtrlCreateCheckbox("Test", 32, 8, 97, 17)
Global $Button1 = GUICtrlCreateButton("Write", 32, 32, 75, 25)

GUISetState(@SW_SHOW)

 ; Intercept Windows command messages with out own handler
 GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

Global $App = "Untitled - Notepad"
WinWait($App)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            If $gb_Interrupt > 0 Then
                $gb_Interrupt = 0
            Else
                GUICtrlSetData($Button1, "Stop")
                _Write()
                GUICtrlSetData($Button1, "Write")
            EndIf
    EndSwitch
WEnd

Func _Write()
    $gb_Interrupt = 1
    While 1
        ControlSend($App, "", "Edit1", "{5}")
        Sleep(2000)
        If $bGetOut Or $gb_Interrupt = 2 Then ExitLoop ; Check if the flag was altered.
    WEnd

    $bGetOut = False ; Reset the flag in case you need to get out again later.
EndFunc   ;==>_Write

Func GetOut()
    $bGetOut = True ; Set the trigger.
EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
     #forceref $hWnd, $Msg, $lParam
     If BitAND($wParam, 0x0000FFFF) = $Button1 And $gb_Interrupt = 1 Then $gb_Interrupt = 2
     Return "GUI_RUNDEFMSG" ; $GUI_RUNDEFMSG
 EndFunc   ;==>_WM_COMMAND

;

I haven't got all my code snippets available to me right now. There should be a more elegant solution.

That's really a nice modification, 

 

You may want to look at the autoit wiki tutorials.  This isn't really a step by step hold your hand while you learn type of thing.

I think ask at forum is better than learning myself with the bot. 

1 person likes this

Share this post


Link to post
Share on other sites
I think ask at forum is better than learning myself with the bot. 

I was just about to offer some help too, until I saw that bad word.

One presumes you have read the Forum Rules about Game Automation?

See the link, at bottom right of every forum page, second link in.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

is it possible to stop the loop with the same button?

I believe so, take a look at >this pause function for use with gui button, and apply the same logic.

Edit: Actually the logic was a little different

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Here is czardas example modified to get out with hotkey or pressing the button again.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $g_bGetOut = False ; Declare a global Flag.
HotKeySet("{F2}", "GetOut") ; Set a hotkey to call a function.

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Test", 146, 66, 268, 198)
Global $Checkbox1 = GUICtrlCreateCheckbox("Test", 32, 8, 97, 17)
Global $Button1 = GUICtrlCreateButton("Write", 32, 32, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $App = "Untitled - Notepad"
WinWait($App)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Write()

    EndSwitch
WEnd

Func _Write()
    $Timer = TimerInit()
    While GUIGetMsg() <> $Button1
        If TimerDiff($Timer) >= 1000 Then
            ControlSend($App, "", "Edit1", "{5}")
            $Timer = TimerInit()
        EndIf
        If $g_bGetOut Then ExitLoop ; Check if the flag was altered.
    WEnd
    $g_bGetOut = False ; Reset the flag in case you need to get out again later.
EndFunc   ;==>_Write

Func GetOut()
    $g_bGetOut = True ; Set the trigger.
EndFunc   ;==>GetOut
2 people like this

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

JohnOne - That's a lot simpler than using GUIRegisterMsg(). I only thought of using the method in my second post, which I've used in several situations. A long complex process would make your solution less than ideal, but for this situation it seems fine. I see you needed to replace the blocking function Sleep() to achieve this. Good example BTW. :)

TheSaint - What bot are you talking about? :ermm:

Edited by czardas

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

I was just about to offer some help too, until I saw that bad word.

One presumes you have read the Forum Rules about Game Automation?

See the link, at bottom right of every forum page, second link in.

ofc i read that, sorry if bot is bad word, but what i ask here is simple task not game automation

The bot i am implying is that autoit wiki bot

 

It's better to learn with human rather than robot.

 

Here is czardas example modified to get out with hotkey or pressing the button again.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $g_bGetOut = False ; Declare a global Flag.
HotKeySet("{F2}", "GetOut") ; Set a hotkey to call a function.

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Test", 146, 66, 268, 198)
Global $Checkbox1 = GUICtrlCreateCheckbox("Test", 32, 8, 97, 17)
Global $Button1 = GUICtrlCreateButton("Write", 32, 32, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $App = "Untitled - Notepad"
WinWait($App)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Write()

    EndSwitch
WEnd

Func _Write()
    $Timer = TimerInit()
    While GUIGetMsg() <> $Button1
        If TimerDiff($Timer) >= 1000 Then
            ControlSend($App, "", "Edit1", "{5}")
            $Timer = TimerInit()
        EndIf
        If $g_bGetOut Then ExitLoop ; Check if the flag was altered.
    WEnd
    $g_bGetOut = False ; Reset the flag in case you need to get out again later.
EndFunc   ;==>_Write

Func GetOut()
    $g_bGetOut = True ; Set the trigger.
EndFunc   ;==>GetOut

This script is great, and start and stop the loop for what i need. Really learn a lot here, thanks.

 

JohnOne - That's a lot simpler than using GUIRegisterMsg(). I only thought of using the method in my second post, which I've used in several situations. A long complex process would make your solution less than ideal, but for this situation it seems fine. I see you needed to replace the blocking function Sleep() to achieve this. Good example BTW. :)

TheSaint - What bot are you talking about? :ermm:

Thanks for your script too, but it's not really stable.

btw, because i am new here, i want to know if i want to ask more thing unrelated to topic , should i create new topic or just ask all of them here?

I want to ask about unzip now, found this looks great >LINK , but i wonder if that unzip support for password?

Edited by lucius100

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

I'm just waiting for clarification of the OP's use of the word, and would have thought it judicious for no-one else to respond until the OP does so, unless they know something i don't.

EDIT

Which OP just did.

Edited by TheSaint

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

btw, because i am new here, i want to know if i want to ask more thing unrelated to topic , should i create new topic or just ask all of them here?

I want to ask about unzip now, found this looks great >LINK , but i wonder if that unzip support for password?

New.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Thanks for your script too, but it's not really stable.

 

In what sense? I guess you must be talking about inconsistant delays caused by Sleep(). Smaller sleep intervals in a nested loop, or using a timer as JohnOne has done, would fix this. If that's not it, then tell me what you are talking about. The interrupt process I used works well for intensive processing - it actually interrupts the script. If that happens when the script is sleeping, it returns to the same place in the script as before, in this case causing a delay. Basically, responce times need some fine tuning.

Edited by czardas

Share this post


Link to post
Share on other sites

In what sense? I guess you must be talking about inconsistant delays caused by Sleep(). Smaller sleep intervals in a nested loop, or using a timer as JohnOne has done, would fix this. If that's not it, then tell me what you are talking about. The interrupt process I used works well for intensive processing - it actually interrupts the script. If that happens when the script is sleeping, it returns to the same place in the script as before, in this case causing a delay.

Since i am not really good at scripting yet, if the way i am talking is rude ,then i apologize, so don't get mad about this. Really English just not my main language.

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

@lucius100 - being the nice guy I am, I will take your word about the bot. :)

The adlib functions might be of help to you. See the Help file.

I've >posted a script (etc), where I've recently added a STOP function to the Query loop, changing the QUERY button text to STOP, while the Query loop is in progress. That uses the earlier AutoIt version names for those functions, but there are plenty of other examples here at the forum.

There are a few other methods instead of adlib, like an extra EXE, that is a floating STOP button, where you can have a shared Registry or INI file key & value, etc.

That latter option, if you can abide having a floating STOP button, is probably the most perfect, as it is quick and independent. It doesn't suffer issues where a user may click the STOP button additional times before it gets disabled, and thus have the process start up again, and again.

I sometimes use CTRL or SHIFT as a hotkey stop & exit.

Edited by TheSaint

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Since i am not really good at scripting yet, if the way i am talking is rude ,then i apologize, so don't get mad about this. Really English just not my main language.

Don't worry. I just wanted to know what you meant by unstable.

Here's what I mean: now there's a maximum of 50 ms delay which gives much better response times.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $bGetOut = False ; Declare a global Flag.
Global $gb_Interrupt = 0 ; As above
HotKeySet("{F2}","GetOut") ; Set a hotkey to call a function.

Global $Form1 = GUICreate("Test", 146, 66, 268, 198)
Global $Checkbox1 = GUICtrlCreateCheckbox("Test", 32, 8, 97, 17)
Global $Button1 = GUICtrlCreateButton("Write", 32, 32, 75, 25)

GUISetState(@SW_SHOW)

 ; Intercept Windows command messages with out own handler
 GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

Global $App = "Untitled - Notepad"
WinWait($App)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            If $gb_Interrupt > 0 Then
                $gb_Interrupt = 0
            Else
                GUICtrlSetData($Button1, "Stop")
                _Write()
                GUICtrlSetData($Button1, "Write")
            EndIf
    EndSwitch
WEnd

Func _Write()
    $gb_Interrupt = 1
    While 1
        ControlSend($App, "", "Edit1", "{5}")
        For $i = 1 To 40 ; 40 x 50 = 2000
            Sleep(50)
            If $bGetOut Or $gb_Interrupt = 2 Then ExitLoop 2 ; Check if the flag was altered.
        Next
    WEnd

    $bGetOut = False ; Reset the flag in case you need to get out again later.
EndFunc   ;==>_Write

Func GetOut()
    $bGetOut = True ; Set the trigger.
EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
     #forceref $hWnd, $Msg, $lParam
     If BitAND($wParam, 0x0000FFFF) = $Button1 And $gb_Interrupt = 1 Then $gb_Interrupt = 2
     Return "GUI_RUNDEFMSG" ; $GUI_RUNDEFMSG
 EndFunc   ;==>_WM_COMMAND

Both methods are fine - they each have their strengths and weaknesses.

Edited by czardas

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0