Jump to content

OnEvent Mode and more than one loop!


 Share

Go to solution Solved by BrewManNH,

Recommended Posts

Hello everybody!

I need to run two codes in separate loops, one in the main window and another when the second window is called, all in OnEvent mode!

But in practice, when the second window opens, the second loop somehow disables "events", see the sample code below:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Global $hGUI1, $hGUI2 = 9999, $hButton1, $hButton2, $hButton3 = 9999 ; Predeclare the variables with dummy values to prevent firing the Case statements

gui1()

Func gui1()
    $hGUI1 = GUICreate("Gui 1", 200, 200, 100, 100)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function
    $hButton1 = GUICtrlCreateButton("Msgbox 1", 10, 10, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
    $hButton2 = GUICtrlCreateButton("Show Gui 2", 10, 60, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
    GUISetState()

    While 1
        ; Here I need to run code inside this loop...
        Sleep(10)
    WEnd
EndFunc   ;==>gui1

Func gui2()
    $hGUI2 = GUICreate("Gui 2", 200, 200, 350, 350)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function
    $hButton3 = GUICtrlCreateButton("MsgBox 2", 10, 10, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
    GUISetState()

    While 1
        ; Here I also need to run another code within this loop!!!
        Sleep(10)
    WEnd
EndFunc   ;==>gui2

Func On_Close()
    Switch @GUI_WinHandle ; See which GUI sent the CLOSE message
        Case $hGUI1
            Exit ; If it was this GUI - we exit <<<<<<<<<<<<<<<
        Case $hGUI2
            GUIDelete($hGUI2) ; If it was this GUI - we just delete the GUI <<<<<<<<<<<<<<<
            GUICtrlSetState($hButton2, $GUI_ENABLE)
    EndSwitch
EndFunc   ;==>On_Close

Func On_Button()
    Switch @GUI_CtrlId ; See which button sent the message
        Case $hButton1
            MessageBox(1) ; We can call a function with parameters here <<<<<<<<<<<<<<<<<<<
        Case $hButton2
            GUICtrlSetState($hButton2, $GUI_DISABLE)
            gui2()
        Case $hButton3
            MessageBox(2) ; We can call a function with parameters here <<<<<<<<<<<<<<<<<<<
    EndSwitch
EndFunc   ;==>On_Button

Func MessageBox($iIndex)
    MsgBox("", "MsgBox " & $iIndex, "Test from Gui " & $iIndex)
EndFunc   ;==>MessageBox

I appreciate all the help,

JS

Edited by JScript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

  • Solution

When using OnEvent mode, you can't use the events to interrupt a running function like that, nothing will be actioned from the first function until you exit the second one. And nothing will get acted on until the third one is finished etc. Your gui1 function jumps to the On_Button function, and doesn't return because it calls gui2 and gets stuck there.

Try it this way.

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Global $hGUI1, $hGUI2 = 9999, $hButton1, $hButton2, $hButton3 = 9999 ; Predeclare the variables with dummy values to prevent firing the Case statements

gui1()
While 1
    ; Here I need to run code inside this loop...
    Sleep(10)
WEnd

Func gui1()
    $hGUI1 = GUICreate("Gui 1", 200, 200, 100, 100)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close", $hGUI1) ; Call a common GUI close function
    $hButton1 = GUICtrlCreateButton("Msgbox 1", 10, 10, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
    $hButton2 = GUICtrlCreateButton("Show Gui 2", 10, 60, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
    GUISetState()
EndFunc   ;==>gui1

Func gui2()
    $hGUI2 = GUICreate("Gui 2", 200, 200, 350, 350)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hGUI2 = ' & $hGUI2 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close", $hGUI2) ; Call a common GUI close function
    $hButton3 = GUICtrlCreateButton("MsgBox 2", 10, 10, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
    GUISetState()
EndFunc   ;==>gui2

Func On_Close()
    Switch @GUI_WinHandle ; See which GUI sent the CLOSE message
        Case $hGUI1
            Exit ; If it was this GUI - we exit <<<<<<<<<<<<<<<
        Case $hGUI2
            GUIDelete($hGUI2) ; If it was this GUI - we just delete the GUI <<<<<<<<<<<<<<<
            GUICtrlSetState($hButton2, $GUI_ENABLE)
    EndSwitch
EndFunc   ;==>On_Close

Func On_Button()
    Switch @GUI_CtrlId ; See which button sent the message
        Case $hButton1
            MessageBox(1) ; We can call a function with parameters here <<<<<<<<<<<<<<<<<<<
        Case $hButton2
            GUICtrlSetState($hButton2, $GUI_DISABLE)
            gui2()
        Case $hButton3
            MessageBox(2) ; We can call a function with parameters here <<<<<<<<<<<<<<<<<<<
    EndSwitch
EndFunc   ;==>On_Button

Func MessageBox($iIndex)
    MsgBox("", "MsgBox " & $iIndex, "Test from Gui " & $iIndex)
EndFunc   ;==>MessageBox

You'd need to probably create other functions that are run from either the While loop or from inside one or both of your other functions, but realize that nothing will get acted on until the functions end.

You could also use a Windows message handler to handle some of it as well without having to use OnEvent or message loop mode. See the link to the GUIRegisterMsg demo I have in my signature that demonstrates how to do that.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Yes, it is an alternative, but unfortunately I can not run the two code snippets in one loop...

Edit:

I think in this form will be the only way that will work!

I'll try, thank you!

JS

Edited by JScript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

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