TAMIRI Posted April 7, 2018 Posted April 7, 2018 Hello, i was wondering how could i stop a loop with a button here is my code #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= Global $Form1 = GUICreate("Form1", 623, 444, 192, 114) Global $StartButton = GUICtrlCreateButton("Button1", 88, 192, 171, 25) Global $EndButton = GUICtrlCreateButton("Button2", 320, 192, 203, 25) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $var = 1 While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $StartButton Start() Case $EndButton Stop() EndSwitch WEnd Func Start() While $var = 1 ConsoleWrite("again" & @CRLF) WEnd EndFunc Func Stop() $var = 0 EndFunc i was reading https://www.autoitscript.com/wiki/Interrupting_a_running_function but still i could not figure it out help PLZ !
Moderators Melba23 Posted April 7, 2018 Moderators Posted April 7, 2018 TAMIRI, Not too difficult: expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Global $bInterrupt = False Global $Form1 = GUICreate("Form1", 623, 444, 192, 114) Global $StartButton = GUICtrlCreateButton("Start", 88, 192, 171, 25) Global $EndButton = GUICtrlCreateButton("Stop", 320, 192, 203, 25) GUICtrlSetState($EndButton, $GUI_DISABLE) GUISetState(@SW_SHOW) GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $StartButton Start() EndSwitch WEnd Func Start() $bInterrupt = False GUICtrlSetState($StartButton, $GUI_DISABLE) GUICtrlSetState($EndButton, $GUI_ENABLE) While $bInterrupt = False Sleep(50) ConsoleWrite("again" & @CRLF) WEnd GUICtrlSetState($StartButton, $GUI_ENABLE) GUICtrlSetState($EndButton, $GUI_DISABLE) $bInterrupt = False EndFunc ;==>Start Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam) ; The Stop button was pressed so set the flag If BitAND($wParam, 0x0000FFFF) = $EndButton Then $bInterrupt = True EndIf Return $GUI_RUNDEFMSG EndFunc ;==>_WM_COMMAND The tutorial explains what is going on. M23 TAMIRI 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
TAMIRI Posted April 8, 2018 Author Posted April 8, 2018 @Melba23, How about if i changed the while loop with For loop i managed to solve it with this expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Global $Form1 = GUICreate("Form1", 623, 444, 192, 114) Global $StartButton = GUICtrlCreateButton("Start", 88, 192, 171, 25) Global $EndButton = GUICtrlCreateButton("Stop", 320, 192, 203, 25) GUICtrlSetState($EndButton, $GUI_DISABLE) GUISetState(@SW_SHOW) GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") Global $bInterrupt = False While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $StartButton Start() EndSwitch WEnd Func Start() $bInterrupt = False GUICtrlSetState($StartButton, $GUI_DISABLE) GUICtrlSetState($EndButton, $GUI_ENABLE) For $x = 1 to 1000 If $bInterrupt = False Then Sleep(500) ConsoleWrite("again " & $x & @CRLF) EndIf Next GUICtrlSetState($StartButton, $GUI_ENABLE) GUICtrlSetState($EndButton, $GUI_DISABLE) $bInterrupt = False EndFunc ;==>Start Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam) ; The Stop button was pressed so set the flag If BitAND($wParam, 0x0000FFFF) = $EndButton Then $bInterrupt = True EndIf Return $GUI_RUNDEFMSG EndFunc ;==>_WM_COMMAND Now, just wondering if you can do better with the for loop
Moderators Melba23 Posted April 8, 2018 Moderators Posted April 8, 2018 TAMIRI, I would have thought this better - you quit the For...Next loop immediately, whereas your version runs the loop for 1000 times regardless: For $x = 1 to 1000 If $bInterrupt Then ExitLoop Else Sleep(500) ConsoleWrite("again " & $x & @CRLF) EndIf Next M23 TAMIRI, tbnghia and Skysnake 1 2 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
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