Jump to content

Script stops responding to OnEvents


 Share

Recommended Posts

I created a GUI window that has a few buttons that trigger functions when they are pressed using GUICtrlSetOnEvent. One of the buttons is a simple Test button. In the function that is called on the Test button, I want to wait for the user to press one of the other buttons to continue. But, once the Test button is pressed none of the other buttons respond any more. Here is some code demonstrating the problem. You should be able to run this code.

#include <GuiConstantsEx.au3>
#include <ButtonConstants.au3>

;---------------------------------- Begin main script ----------------------------------
Global $StartPressed
Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode 

GuiCreate("Add data", 440, 125, 700, 20)
GUISetOnEvent($GUI_EVENT_CLOSE, "OnExitScript")
$StartButton = GUICtrlCreateButton("Start", 235, 90, 60, 25, BitOr($GUI_SS_DEFAULT_BUTTON, $BS_DEFPUSHBUTTON))
$TestButton = GUICtrlCreateButton("Test", 300, 90, 60, 25)
$ExitButton = GUICtrlCreateButton("Exit", 365, 90, 60, 25)
GUICtrlSetOnEvent($StartButton, "OnStartPressed")
GUICtrlSetOnEvent($TestButton, "OnTestPressed")
GUICtrlSetOnEvent($ExitButton, "OnExitPressed")
HotKeySet("{PAUSE}", "OnTestPressed")
GuiSetState()

$StartPressed = 0
While $StartPressed <> 1
    Sleep(100)
WEnd

;Must use Close Button on window to exit script
While 1
    Sleep(1000)
WEnd
;---------------------------------- End main script ----------------------------------

;This function is done if the user presses the Exit button or closes the window
Func OnExitScript()
    Sleep(200)
    Exit
EndFunc

;This function is done if the user presses the Start button
Func OnExitPressed()
    MsgBox(0,"Test", "ExitPressed")
EndFunc

;This function is done if the user presses the Start button
Func OnStartPressed()
    MsgBox(0,"Test", "StartPressed")
    $StartPressed = 1
EndFunc

;This function is done if the user presses the Test button
Func OnTestPressed()
    MsgBox(0,"Test", "TestPressed")
    $StartPressed = 0
    While $StartPressed <> 1
        Sleep(100)  ;############# It seems to be stuck here, why? ################
    WEnd
    MsgBox(0,"Test", "TestPressed exit function")
EndFunc
Link to comment
Share on other sites

Functions triggered by events cannot be interrupted by other events. The next event will only be handled after the first function returns and as your function will not return unless an event is handled you're stuck.

A way around this is to make the first function return, but trigger another function that can be interrupted by events like this:

#include <GuiConstantsEx.au3>
#include <ButtonConstants.au3>

;---------------------------------- Begin main script ----------------------------------
Global $StartPressed, $fRunTest
Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode

GuiCreate("Add data", 440, 125, 700, 20)
GUISetOnEvent($GUI_EVENT_CLOSE, "OnExitScript")
$StartButton = GUICtrlCreateButton("Start", 235, 90, 60, 25, BitOr($GUI_SS_DEFAULT_BUTTON, $BS_DEFPUSHBUTTON))
$TestButton = GUICtrlCreateButton("Test", 300, 90, 60, 25)
$ExitButton = GUICtrlCreateButton("Exit", 365, 90, 60, 25)
GUICtrlSetOnEvent($StartButton, "OnStartPressed")
GUICtrlSetOnEvent($TestButton, "OnTestPressed")
GUICtrlSetOnEvent($ExitButton, "OnExitPressed")
HotKeySet("{PAUSE}", "OnTestPressed")
GuiSetState()

$StartPressed = 0
While $StartPressed <> 1
    Sleep(100)
WEnd

;Must use Close Button on window to exit script
While 1
    If $fRunTest Then StartTestPressed() ;Check for the flag
    Sleep(1000)
WEnd
;---------------------------------- End main script ----------------------------------

;This function is done if the user presses the Exit button or closes the window
Func OnExitScript()
    Sleep(200)
    Exit
EndFunc

;This function is done if the user presses the Start button
Func OnExitPressed()
    MsgBox(0,"Test", "ExitPressed")
EndFunc

;This function is done if the user presses the Start button
Func OnStartPressed()
    MsgBox(0,"Test", "StartPressed")
    $StartPressed = 1
EndFunc

;This function is done if the user presses the Test button
;helperfunction
Func OnTestPressed()
    $fRunTest = True ;Set a flag to run the actual function.
EndFunc

Func StartTestPressed()
    MsgBox(0,"Test", "TestPressed")
    $StartPressed = 0
    While $StartPressed <> 1
        Sleep(100)  ;############# It seems to be stuck here, why? ################
    WEnd
    MsgBox(0,"Test", "TestPressed exit function")
    $fRunTest = False ;reset the flag
EndFunc
Link to comment
Share on other sites

  • Moderators

texan,

When you are in a function other events only trigger when you return. The Interrupting a running function tutorial in the Wiki tells you how you can get round this. :unsure:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

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