Jump to content

GUIOnEventMode


Recommended Posts

Working with GUIOnEventMode, While the script is looping in a function, it will not detect when/if Pause or $GUI_EVENT_CLOSE messages. I figure the Function must end for GUIOnEventMode to work. So is there a way to read the Gui message from another function and perform an OnEvent?

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)
Opt("GUIOnEventMode", 1)

Local $GUI_EVENT_Run,$GUI_EVENT_Pause,$gui

$gui = GUICreate("CELLUBOT INSTALLER")
GUISetOnEvent($GUI_EVENT_CLOSE, "Terminate")


$GUI_EVENT_Run = GUICtrlCreateButton("Run", 10, 10, 65, 20)
GUICtrlSetOnEvent($GUI_EVENT_Run, "RunApplication")
$GUI_EVENT_Pause = GUICtrlCreateButton("Pause", 95, 10, 65, 20)
GUICtrlSetOnEvent($GUI_EVENT_Pause, "TogglePause")
GUISetState(@SW_SHOW)


While 1
 Sleep(1000)
WEnd

;This is where my script gets lost in itself, and will not detect if the Pause or Terminated buttons have been pressed.
Func RunApplication()
    _FirstFunction()
    _SecondFunction()
    _ThirdFunction()
    RunApplication()
EndFunc

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Link to comment
Share on other sites

Your using

GUISetOnEvent($GUI_EVENT_CLOSE, "Terminate")
But you have not included a
Func Terminate()
;; exit, or some other code you like to run in this case.
EndFunc

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

That was not the entire script but a mere example in which i believe the problem or solution may be found, I do have a Func Terminate() in my script, as with the other listed Functions, that is not the problem. The problem is that when the button "Run" is pressed, it goes to a Function that loops itself, however if the button 'Pause' or the $GUI_EVENT_CLOSE is pressed then nothing happens. It appears that a Function that loops itself, blocks the GUIOnEventMode. I was hoping someone else found a way around this, or even a better way to loop a function until something else happens in the GUI

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Link to comment
Share on other sites

In OnEventMode a called function MUST be finished before the next function can be called by an OnEvent trigger... if you've got a time consuming function started by a button click you'll see that the function is called as often as you click the button, not instantly but in the order of clicks.

To circumvent this I use the Windows Messages. Those will be detected even if you're in a function triggered by OnEvent. Define a Global Pause Bool and check for this within your functions, e.g. in tight loops check for $b_Break = true and then return instantly (btw, I prefer to call my controls something like $c_FUNCTION_TYPE_NAME, makes it easier to find them later on if you script grows).

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Global $b_Run = False
Global $b_Pause = False

$gui = GUICreate("CELLUBOT INSTALLER")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$c_Main_Button_Run = GUICtrlCreateButton("Run", 10, 10, 65, 20)
GUICtrlSetOnEvent(-1, "RunApplication")
$c_Main_Button_Pause = GUICtrlCreateButton("Pause", 95, 10, 65, 20)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")

While 1
    Sleep(1000)
WEnd

;This is where my script gets lost in itself, and will not detect if the Pause or Terminated buttons have been pressed.
Func RunApplication()
    if not $b_Run then Return
    ConsoleWrite(TimerInit() & @tab & "+START" & @crlf)
    _FirstFunction()
EndFunc   ;==>RunApplication

Func _FirstFunction()
    while 1
        sleep(200)
        ConsoleWrite("-" & TimerInit() & @tab & "Working..." & @crlf)
        while $b_Pause
            sleep(200)
            if not $b_Run then ExitLoop
            ConsoleWrite("!" & TimerInit() & @tab & "Pausing..." & @crlf)
        WEnd
        if not $b_Run then
            ConsoleWrite("+" & TimerInit() & @tab & "STOP" & @crlf)
            Return
        endif
    WEnd
EndFunc

Func MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    $nNotifyCode = BitShift($wParam, 16)
    $nID = BitAND($wParam, 0x0000FFFF)
    $hCtrl = $lParam

    Switch $nID
        Case $c_Main_Button_Pause
            $b_Pause = Not $b_Pause
            If $b_Pause Then
                GUICtrlSetData($c_Main_Button_Pause, "Resume")
            Else
                GUICtrlSetData($c_Main_Button_Pause, "Pause")
            EndIf
        Case $c_Main_Button_Run
            $b_Run = Not $b_Run
            If $b_Run Then
                GUICtrlSetData($c_Main_Button_Run, "Stop")
            Else
                GUICtrlSetData($c_Main_Button_Run, "Run")
            EndIf
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_WM_COMMAND

Func _Exit()
    Exit
EndFunc
Link to comment
Share on other sites

Thanks KaFu! I have been playing with this, since yesterday evening with my actual script, took awhile for me to understand how it worked. After hours of fun and frustration, I have a good understanding of it, and amazingly it is working pretty well in my script. Also with how you re-wrote _FirstFunction() was an excellant format, Taking that function from looping itself by calling itself at the end, and reformatting into a While Statement (its ok to laugh at my noobishness, I know I did), fixed a lot of other problems I was facing in my script. So thanks again for an excellent example that not only shown me how to fix the problem on hand, but in hindsight, also many other problems I was having at the same time :blink:

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Link to comment
Share on other sites

After hours of fun and frustration, I have a good understanding of it,

Glad to hear that ;), thought this was one of the usual hit&run questions :blink:...

... from looping itself by calling itself at the end

Function recursion has to be considered truly evil in most circumstances, as it easily leads to endless loops. Sometimes it's the easiest way to do something, but if you use it better hand-over a depth counter (maybe as a function parameter) and build in a break. Max. recursion depth for AutoIt is currently 5100 (though the reference in the help-file says only something of call and executes? Not sure about that limit myself :P).
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...