Sign in to follow this  
Followers 0
MrChiliCheese

GuiSetOnEvent

5 posts in this topic

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!

Share this post


Link to post
Share on other sites



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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

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... :D

I have here three examples, what do you think about those solutions?

Func _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

Share this post


Link to post
Share on other sites

#4 ·  Posted (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: ;)

#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_COMMAND

Any questions? :huh:

I will add something like this to the tutorial later today - it keeps popping up as a question. :)

M23

Edit: Tutorial amended - please let me know what you think of the new section. :)

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Well, that is awesome! I will consider this also for my scripts :-)

Thank you!

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