acers Posted August 6, 2011 Share Posted August 6, 2011 hey i've been trying to figure out how to make the loop() check if a button was pressed but with no luck. can someone shed some light on this subject? i want to exit loop() if a stop button is pressed. $count = 0 While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 If $count = 1 Then GUICtrlSetData($Button1, "Start") $count = 0 ElseIf $count = 0 Then GUICtrlSetData($Button1, "Stop") $count = 1 Loop() EndIf Case $Button2 GUISwitch($Form1_1) GUIDelete() Exit EndSwitch WEnd Func Loop() EndFunc Link to comment Share on other sites More sharing options...
Zedna Posted August 6, 2011 Share Posted August 6, 2011 Maybe something like this: expandcollapse popup$count = 0 $stop = False While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 If $count = 1 Then GUICtrlSetData($Button1, "Start") $count = 0 ElseIf $count = 0 Then GUICtrlSetData($Button1, "Stop") $count = 1 Loop() If $stop Then ... EndIf Case $Button2 GUISwitch($Form1_1) GUIDelete() Exit EndSwitch WEnd Func Loop() $stop = False While ... $nMsg = GUIGetMsg() If $nMsg = $Button1 Then $stop = True Return EndIf ... WEnd EndFunc Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
acers Posted August 6, 2011 Author Share Posted August 6, 2011 (edited) it's not working. is it because i'm using the same button for start and stop? edit: here is the full code. if anyone can get it to work i would be really grateful. expandcollapse popup#include <ButtonConstants.au3> #include <ComboConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <GuiStatusBar.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> $Form1_1 = GUICreate("Form1", 539, 466, 192, 124) $Button1 = GUICtrlCreateButton("Start", 88, 24, 97, 41) $Button2 = GUICtrlCreateButton("Exit", 192, 24, 97, 41) GUISetState(@SW_SHOW) $count = 0 While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 If $count = 1 Then GUICtrlSetData($Button1, "Start") $count = 0 ElseIf $count = 0 Then GUICtrlSetData($Button1, "Stop") $count = 1 Loop() EndIf Case $Button2 GUISwitch($Form1_1) GUIDelete() Exit EndSwitch WEnd Func Loop() EndFunc Edited August 6, 2011 by acers Link to comment Share on other sites More sharing options...
PsaltyDS Posted August 6, 2011 Share Posted August 6, 2011 Once you go into Loop(), the GUI messages never get checked again. Another possibility: #include <GUIConstantsEx.au3> Global $Form1_1, $Button1, $Button2, $f_Run = False $Form1_1 = GUICreate("Form1", 539, 466, 192, 124) $Button1 = GUICtrlCreateButton("Start", 88, 24, 97, 41) $Button2 = GUICtrlCreateButton("Exit", 192, 24, 97, 41) GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $Button1 $f_Run = Not $f_Run Case $Button2 Exit EndSwitch If $f_Run Then ; Do one cycle of whatever... ; Should be something that doesn't take long or GUI will be unresponsive EndIf WEnd The stuff you do inside the "If $f_Run Then" condition can still make the GUI clunky if it takes long. In that case you want to use events instead. The easiest event is just a HotKeySet(), but if you want to use the GUI put it in GuiOnEventMode: #include <GUIConstantsEx.au3> Opt("GuiOnEventMode", 1) Global $Label1, $f_Run = False GUICreate("Form1", 539, 466, 192, 124) GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit") GUICtrlCreateButton("Start/Stop", 88, 24, 97, 41) GUICtrlSetOnEvent(-1, "_ButtonHit") GUICtrlCreateButton("Exit", 192, 24, 97, 41) GUICtrlSetOnEvent(-1, "_ButtonHit") $Label1 = GUICtrlCreateLabel("Time = 00:00:00", 88, 150, 200, 20) GUISetState(@SW_SHOW) While 1 If $f_Run Then $sTime = "Time = " & @HOUR & ":" & @MIN & ":" & @SEC If GUICtrlRead($Label1) <> $sTime Then GUICtrlSetData($Label1, $sTime) EndIf Sleep(20) WEnd Func _quit() Exit EndFunc ;==>_Quit Func _ButtonHit() Switch ControlGetText(@GUI_WinHandle, "", @GUI_CtrlHandle) Case "Start/Stop" $f_Run = Not $f_Run Case "Exit" Exit EndSwitch EndFunc ;==>_ButtonHit Go ahead, use event mode. You know you want to! Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Gui Posted August 6, 2011 Share Posted August 6, 2011 (edited) Try this: expandcollapse popup#include <INet.au3> #include <GDIPlus.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #Include <GuiEdit.au3> #include <Constants.au3> #include <GUImenu.au3> #include <File.au3> #include <Array.au3> Global $running = 0 #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Toothless", 317, 140, 192, 124) $Start = GUICtrlCreateButton("Start", ........) $Stop = GUICtrlCreateButton("Stop", ..........) GUIRegisterMsg($WM_COMMAND,'command') GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Func command($hwnd,$msg,$wparam,$lparam) $nNotifyCode = BitShift($wparam, 16) $nID = BitAND($wparam, 0x0000FFFF) If $nID = $Start Then $running = 1 EndIf If $nID = $Stop Then $running = 0 EndIF EndFunc While 1 $nMsg = GUIGetMsg () Switch $nMsg Case $GUI_EVENT_CLOSE $running = 0 Case $Start _Start() $running = 1 EndSwitch WEnd Func _Start() While 1 If $running = 0 Then Return Wend EndFunc Edited August 6, 2011 by Gui Link to comment Share on other sites More sharing options...
PsaltyDS Posted August 6, 2011 Share Posted August 6, 2011 @Gui: Why would you use so much code to re-invent much simpler native AutoIt functions? Of course you could register and handle every message type yourself, but why would you if you don't have to? Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Gui Posted August 6, 2011 Share Posted August 6, 2011 Why not? Ha in my case, I do it b/c it's useful in complected loops. You could also do: While $running = 1 ;;;; Wend etc. This method also works with GUIGetMsg, instead of OnEvent functions, which I hate. I mean it's up to the coder lol. Just putting up my suggestion. Also, I don't think it's simple at all. There's maybe HotKey, but other than that, completely stopping loops in AutoIt isn't that simple. Link to comment Share on other sites More sharing options...
Zedna Posted August 6, 2011 Share Posted August 6, 2011 http://www.autoitscript.com/wiki/Interrupting_a_running_function Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now