texan Posted April 27, 2011 Share Posted April 27, 2011 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. expandcollapse popup#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 More sharing options...
Tvern Posted April 27, 2011 Share Posted April 27, 2011 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: expandcollapse popup#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 More sharing options...
Moderators Melba23 Posted April 27, 2011 Moderators Share Posted April 27, 2011 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. 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...
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