MrChiliCheese Posted February 6, 2013 Share Posted February 6, 2013 Maybe I'm to stupid, but is this correct: If I'm launching a function with GuiCtrlSetOnEvent I'm stuck in this function and cannot intercept it in anyway with an GuiSetOnEvent until the function called by GuiCtrlSetOnEvent is finished! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 6, 2013 Moderators Share Posted February 6, 2013 MrChiliCheese,Not strictly true - there is one case where you can interrupt the running function easily, but usually you need somehting more complex. See the Interrupting a running function tutorial in the Wiki for more details. And by the way, the same is true for MessageLoop mode as well except that there is not even that one exception - as the tutorial explains. M23 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 Link to comment Share on other sites More sharing options...
MrChiliCheese Posted February 7, 2013 Author Share Posted February 7, 2013 MrChiliCheese, Not strictly true - there is one case where you can interrupt the running function easily, but usually you need somehting more complex. See the Interrupting a running function tutorial in the Wiki for more details. And by the way, the same is true for MessageLoop mode as well except that there is not even that one exception - as the tutorial explains. M23 Thank you Melba, wasn't even aware of the tutorial, but my problem was another one - even with changes from the tutorial: If you are going "blocking" function like Sleep or WinWait your're going to have a bad time... I have here three examples, what do you think about those solutions? expandcollapse popupFunc _Example_not_good() WinWait("") If $interrrupted Then Return ControlClick("", "", "") WinWait("") If $interrrupted Then Return ControlClick("", "", "") WinWait("") If $interrrupted Then Return ControlClick("", "", "") EndFunc ;==>_Example_not_good Func _Example_ok() Do Sleep(25) If $interrrupted Then Return Until WinExists("") ControlClick("", "", "") Do Sleep(25) If $interrrupted Then Return Until WinExists("") ControlClick("", "", "") EndFunc ;==>_Example_ok Func _Example_good() Local Static $iStep While Not $interrrupted Switch $iStep Case 0 If WinExists("") Then ControlClick("", "", "") $iStep = 10 EndIf Case 10 If WinExists("") Then ControlClick("", "", "") $iStep = 20 EndIf Case 20 If WinExists("") Then ControlClick("", "", "") $iStep = 0 EndIf EndSwitch WEnd If $interrrupted Then $iStep = 0 Return EndIf EndFunc ;==>_Example_good Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 7, 2013 Moderators Share Posted February 7, 2013 (edited) MrChiliCheese,If you want a long Sleep or some other function which waits for a fair time then you need to put it into a wrapper function like this so that you can look for the interrupt at regular intervals: expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> ; Set a HotKey HotKeySet("x", "_Interrupt_HotKey") ; Declare a flag $fInterrupt = 0 Opt("GUIOnEventMode", 1) $hGUI = GUICreate("Test", 500, 500) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit") $hButton_1 = GUICtrlCreateButton("Func One", 10, 10, 80, 30) GUICtrlSetOnEvent($hButton_1, "_Func_1") $hButton_2 = GUICtrlCreateButton("Func Two", 10, 50, 80, 30) GUICtrlSetOnEvent($hButton_2, "_Func_2") ; Create a dummy control for the Accelerator to action $hAccelInterupt = GUICtrlCreateDummy() GUICtrlSetOnEvent($hAccelInterupt, "_Interrupt_Accel") ; Set an Accelerator key to action the dummy control Dim $AccelKeys[1][2] = [["z", $hAccelInterupt]] GUISetAccelerators($AccelKeys) GUISetState() ; Intercept Windows command messages with out own handler GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") While 1 Sleep(10) WEnd Func _Func_1() ; Make sure the flag is cleared $fInterrupt = 0 For $i = 1 To 20 ConsoleWrite("-Func 1 Running" & @CRLF) ; Run a modified Sleep function If _Interrupt_Sleep(5000) Then ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ; The flag was set Switch $fInterrupt Case 1 ConsoleWrite("!Func 1 interrrupted by Func 2" & @CRLF) Case 2 ConsoleWrite("!Func 1 interrrupted by HotKey" & @CRLF) Case 3 ConsoleWrite("!Func 1 interrrupted by Accelerator" & @CRLF) EndSwitch Return EndIf Sleep(100) Next ConsoleWrite(">Func 1 Ended" & @CRLF) EndFunc ;==>_Func_1 Func _Func_2() For $i = 1 To 3 ConsoleWrite("+Func 2 Running" & @CRLF) Sleep(100) Next ConsoleWrite(">Func 2 Ended" & @CRLF) EndFunc ;==>_Func_2 Func _Exit() Exit EndFunc ;==>_Exit Func _Interrupt_Sleep($iDelay) ; Get a timestamp Local $iBegin = TimerInit() Do ; A minium Sleep (could also be a WinWaitActive with a timeout) Sleep(10) ; Look for the interrrupt If $fInterrupt Then ; And return True immediately if set Return True EndIf Until TimerDiff($iBegin) > $iDelay ; Return False if interrupt not set Return False EndFunc ;==>_Interrupt_Sleep Func _Interrupt_HotKey() ; The HotKey was pressed so set the flag $fInterrupt = 2 EndFunc ;==>_Interrupt_HotKey Func _Interrupt_Accel() ; This is an empty function for the dummy control linked to the Accelerator key EndFunc ;==>_Interrupt_Accel Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam) ; The Func 2 button was pressed so set the flag If BitAND($wParam, 0x0000FFFF) = $hButton_2 Then $fInterrupt = 1 ; The dummy control was actioned by the Accelerator key so set the flag If BitAND($wParam, 0x0000FFFF) = $hAccelInterupt Then $fInterrupt = 3 Return $GUI_RUNDEFMSG EndFunc ;==>_WM_COMMANDAny questions? I will add something like this to the tutorial later today - it keeps popping up as a question. M23Edit: Tutorial amended - please let me know what you think of the new section. Edited February 7, 2013 by Melba23 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 Link to comment Share on other sites More sharing options...
MrChiliCheese Posted February 7, 2013 Author Share Posted February 7, 2013 Well, that is awesome! I will consider this also for my scripts :-) Thank you! 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