Jump to content
Sign in to follow this  
xilace

Exiting out of a loop/sleep

Recommended Posts

xilace

This is a snipit of a program I'm attempting to make. I'm having issues though after you press the start button and wish to stop it. What i'm trying to do is once you press start it would continue going through the function with the loop until the user presses the pause or cancel button. I'm sure i'm overlooking something rather simple with this.

Opt("SendKeyDelay", 100) ;100 milliseconds
Opt("PixelCoordMode", 2) ;1=absolute, 0=relative, 2=client

Global $Paused
HotKeySet("{PAUSE}", "TogglePause") ;Press Pause/Break to pause the script
HotKeySet("{END}", "Terminate") ;Press END key to stop the script


Opt('GUIOnEventMode',True)
;set the default Width, height, X & Y coords
Global $Width = ('460')
Global $Height = ('285')
Global $X = ('611')
Global $Y = ('389')



#Region Main Window
$Main = GUICreate("Testing function", $width, $Height, $X, $Y)
GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit', $Main)
$Start_Main = GUICtrlCreateButton("&Start",100, 220, 75, 30, 0)
GUICtrlSetOnEvent($Start_Main, "_Start")
$Pause_Main = GUICtrlCreateButton("Pause", 200, 220, 75, 30, 0)
GUICtrlSetOnEvent($Pause_Main, "_Pause")
$Cancel_Main = GUICtrlCreateButton("&Cancel", 300, 220, 75, 30, 0)
GUICtrlSetOnEvent($Cancel_Main, "_Exit")
$List_Main = GUICtrlCreateList("", 40, 40, 350, 150, BitOR($GUI_SS_DEFAULT_LIST, $LBS_DISABLENOSCROLL, $LBS_NOSEL))
$File_Menu = GUICtrlCreateMenu("&File")
$Exit_Item = GUICtrlCreateMenuItem("Exit", $File_Menu)
GUICtrlSetOnEvent($Exit_Item, "_Exit")
$Help_Menu = GUICtrlCreateMenu("&Help")
$About_Item = GUICtrlCreateMenuItem("About", $Help_Menu)
GUICtrlSetOnEvent($About_Item, "_About")
GUISetState(@SW_SHOW)
#EndRegion END Main Window




;This keeps the program running until its exited
While 1
$msg = GUIGetMsg()
If $msg = $Exit_Item Or $msg = -3 Or $msg = -1 Then
ExitLoop
EndIf
Sleep(20)
WEnd




Func _Start()
Do
;update list to show its checking
GUICtrlSetData($List_Main, "Checking for new keys.......... " & _NowTime() & "|")
Sleep(10000)
Until @error
EndFunc


;Function for the pause button
Func _Pause()
GUICtrlSetData($List_Main, "Pausing " & _NowTime() & "|")
Send("{PAUSE}")
EndFunc


;Function to close the entire program
Func _Exit()
Exit
EndFunc


;Function to close the entire program
Func _Cancel()
Exit
EndFunc




Func TogglePause()
$Paused = NOT $Paused
While $Paused
sleep(100)
ToolTip('Script is "Paused"',0,0)
WEnd
ToolTip("")
EndFunc

Func Terminate()
Exit 0
EndFunc

Share this post


Link to post
Share on other sites
DicatoroftheUSA

Instead of putting a huge sleep for your "checking" have a variable the counts and that triggers the event and then resets after x amount of times.

Or use _timer_init() and _timer_diff

Edited by DicatoroftheUSA

Share this post


Link to post
Share on other sites
xilace

Is there any way you might be able to give an example? I'm trying it and having some difficulties with the proper setup.

Share this post


Link to post
Share on other sites
n1maS

Well, you can use AdlibRegister instead sleep.

#include <ListboxConstants.au3>
#include <GUIConstantsEx.au3>
#include <Date.au3>

Opt("SendKeyDelay", 100) ;100 milliseconds
Opt("PixelCoordMode", 2) ;1=absolute, 0=relative, 2=client

Global $Paused
HotKeySet("{PAUSE}", "TogglePause") ;Press Pause/Break to pause the script
HotKeySet("{END}", "Terminate") ;Press END key to stop the script


Opt('GUIOnEventMode', True)
;set the default Width, height, X & Y coords
Global $Width = ('460')
Global $Height = ('285')
Global $X = ('611')
Global $Y = ('389')



#region Main Window
$Main = GUICreate("Testing function", $Width, $Height, $X, $Y)
GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit', $Main)
$Start_Main = GUICtrlCreateButton("&Start", 100, 220, 75, 30, 0)
GUICtrlSetOnEvent($Start_Main, "_Start")
$Pause_Main = GUICtrlCreateButton("Pause", 200, 220, 75, 30, 0)
GUICtrlSetOnEvent($Pause_Main, "_Pause")
$Cancel_Main = GUICtrlCreateButton("&Cancel", 300, 220, 75, 30, 0)
GUICtrlSetOnEvent($Cancel_Main, "_Exit")
$List_Main = GUICtrlCreateList("", 40, 40, 350, 150, BitOR($GUI_SS_DEFAULT_LIST, $LBS_DISABLENOSCROLL, $LBS_NOSEL))
$File_Menu = GUICtrlCreateMenu("&File")
$Exit_Item = GUICtrlCreateMenuItem("Exit", $File_Menu)
GUICtrlSetOnEvent($Exit_Item, "_Exit")
$Help_Menu = GUICtrlCreateMenu("&Help")
$About_Item = GUICtrlCreateMenuItem("About", $Help_Menu)
GUICtrlSetOnEvent($About_Item, "_About")
GUISetState(@SW_SHOW)
#endregion Main Window




;This keeps the program running until its exited
While 1
$msg = GUIGetMsg()
If $msg = $Exit_Item Or $msg = -3 Or $msg = -1 Then
ExitLoop
EndIf
Sleep(20)
WEnd


Func _Start()
GUICtrlSetData($List_Main, "Checking for new keys.......... " & _NowTime() & "|")
AdlibRegister("__Update", 10000)
EndFunc ;==>_Start


Func __Update()
GUICtrlSetData($List_Main, "Checking for new keys.......... " & _NowTime() & "|")
EndFunc ;==>__Update


;Function for the pause button
Func _Pause()
AdlibUnRegister("__Update")
GUICtrlSetData($List_Main, "Pausing " & _NowTime() & "|")
Send("{PAUSE}")
EndFunc ;==>_Pause


;Function to close the entire program
Func _Exit()
Exit
EndFunc ;==>_Exit


;Function to close the entire program
Func _Cancel()
Exit
EndFunc ;==>_Cancel

Func _About()
EndFunc ;==>_About


Func TogglePause()
$Paused = Not $Paused
While $Paused
Sleep(100)
ToolTip('Script is "Paused"', 0, 0)
WEnd
ToolTip("")
EndFunc ;==>TogglePause

Func Terminate()
Exit 0
EndFunc ;==>Terminate

Share this post


Link to post
Share on other sites
xilace

preliminary testing your recommendation is working PERFECTLY n1maS! thanks a ton, i didnt even know about that ability!

Share this post


Link to post
Share on other sites
DicatoroftheUSA

Is there any way you might be able to give an example? I'm trying it and having some difficulties with the proper setup.

$x=0
While 1
$msg = GUIGetMsg()
If $msg = $Exit_Item Or $msg = -3 Or $msg = -1 Then
ExitLoop
EndIf
Sleep(20)
$x+=1
if $x<500 then ContinueLoop
GUICtrlSetData($List_Main, "Checking for new keys.......... " &amp; _NowTime() &amp; "|")
if @error then
GUICtrlSetData($List_Main, "Error" &amp; @error&amp; "|")
$x=0
;error handling
Else
$x=0
EndIf

WEnd

OR

$hTime=TimerInit()
While 1
$msg = GUIGetMsg()
If $msg = $Exit_Item Or $msg = -3 Or $msg = -1 Then
ExitLoop
EndIf

if TimerDiff($hTime)<10000 then ContinueLoop
$hTime=TimerInit()
GUICtrlSetData($List_Main, "Checking for new keys.......... " &amp; _NowTime() &amp; "|")

WEnd
Edited by DicatoroftheUSA

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  

×