MrFlibble Posted June 18, 2010 Share Posted June 18, 2010 I have a small script that has several controls. What I need is for the gui to start a timer when one control is pressed and wait until the timer reaches a certain time or another control is pressed. Any help would be appreciated as I'm completely stuck on how to exit out of the loop. Ideally it has be using GuiOnEventMode. Sample of what I need is below. expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Opt("GUIOnEventMode", 1) #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 633, 454, 193, 115) $Label1 = GUICtrlCreateLabel("", 164, 186, 310, 99, $SS_CENTER) GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif") $Button1 = GUICtrlCreateButton("Button1", 184, 372, 75, 25, 0) $Button2 = GUICtrlCreateButton("Button2", 300, 368, 75, 25, 0) $Button3 = GUICtrlCreateButton("Button3", 420, 368, 75, 25, 0) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvents") GUICtrlSetOnEvent($Button1, "_GuiCtrlEvents") GUICtrlSetOnEvent($Button2, "_GuiCtrlEvents") GUICtrlSetOnEvent($Button3, "_GuiCtrlEvents") While 1 Sleep(100) WEnd Func _GuiEvents() Switch @GUI_CtrlId Case $GUI_EVENT_CLOSE If @GUI_WinHandle = $Form1 Then Exit EndIf EndSwitch EndFunc Func _GuiCtrlEvents() Select Case @GUI_CtrlId = $Button1 MsgBox(0,"",@GUI_CtrlId) Timer() Case @GUI_CtrlId = $Button2 MsgBox(0,"",@GUI_CtrlId) Case @GUI_CtrlId = $Button3 MsgBox(0,"",@GUI_CtrlId) EndSelect EndFunc func Timer() $begin = TimerInit() Do $dif = TimerDiff($begin) If TimerDiff($begin) > 10 * 1000 Then ExitLoop If $dif > 1000 And $dif < 2000 Then Guictrlsetdata($Label1,"1") EndIf If $dif > 2000 And $dif < 3000 Then Guictrlsetdata($Label1,"2") EndIf If $dif > 3000 And $dif < 4000 Then Guictrlsetdata($Label1,"3") EndIf If $dif > 4000 And $dif < 5000 Then Guictrlsetdata($Label1,"4") EndIf If $dif > 5000 And $dif < 6000 Then Guictrlsetdata($Label1,"5") EndIf If $dif > 6000 And $dif < 7000 Then Guictrlsetdata($Label1,"6") EndIf If $dif > 7000 And $dif < 8000 Then Guictrlsetdata($Label1,"7") EndIf If $dif > 8000 And $dif < 9000 Then Guictrlsetdata($Label1,"8") EndIf If $dif > 9000 And $dif < 10000 Then Guictrlsetdata($Label1,"9") EndIf Sleep(50) Until @GUI_CtrlId <> $Button1 EndFunc Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 18, 2010 Moderators Share Posted June 18, 2010 MrFlibble,I would do it by using an Adlib function like this:expandcollapse popup#include <GUIConstantsEx.au3> Opt("GUIOnEventMode", 1) $hGUI = GUICreate("Test", 500, 500) GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvents") $hLabel = GUICtrlCreateLabel("0", 10, 10, 50, 50) GUICtrlSetFont(-1, 24) $hButton_1 = GUICtrlCreateButton("1", 10, 100, 80, 30) GUICtrlSetOnEvent($hButton_1, "_GuiCtrlEvents") $hButton_2 = GUICtrlCreateButton("2", 110, 100, 80, 30) GUICtrlSetOnEvent($hButton_2, "_GuiCtrlEvents") $hButton_3 = GUICtrlCreateButton("3", 210, 100, 80, 30) GUICtrlSetOnEvent($hButton_3, "_GuiCtrlEvents") GUISetState() While 1 Sleep(10) WEnd Func _GuiEvents() Switch @GUI_CtrlId Case $GUI_EVENT_CLOSE If @GUI_WinHandle = $hGUI Then Exit EndSwitch EndFunc Func _GuiCtrlEvents() Switch @GUI_CtrlId Case $hButton_1 ConsoleWrite(@GUI_CtrlId & @CRLF) ; Start the Adlib function AdlibRegister("_CheckIt", 1000) Case $hButton_2 ConsoleWrite(@GUI_CtrlId & @CRLF) Case $hButton_3 ConsoleWrite(@GUI_CtrlId & @CRLF) EndSwitch EndFunc ;==>_GuiCtrlEvents Func _CheckIt() ; Increase the count GUICtrlSetData($hLabel, GUICtrlRead($hLabel) + 1) ; Check if another control pressed or timer expired If @GUI_CtrlId <> $hButton_1 Or GUICtrlRead($hLabel) = 10 Then ; Cancel Adlib function AdlibUnRegister("_CheckIt") ; Reset counter GUICtrlSetData($hLabel, 0) EndIf EndFuncPlease ask if anything is unclear. 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...
Yoriz Posted June 18, 2010 Share Posted June 18, 2010 Melba23 beat me to it again MrFlibble another way is using the timers udf functoins expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <Timers.au3> HotKeySet("{ESC}", "_exit") Func _exit() Exit EndFunc ;==>_exit Global $iTimerDuration = 10, $iTimerValue = 0, $hTimer Opt("GUIOnEventMode", 1) #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 633, 454, 193, 115) $Label1 = GUICtrlCreateLabel("0", 164, 186, 310, 99, $SS_CENTER) GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif") $Button1 = GUICtrlCreateButton("Start", 184, 368, 75, 25, 0) $Button2 = GUICtrlCreateButton("Stop", 300, 368, 75, 25, 0) $Button3 = GUICtrlCreateButton("Button3", 420, 368, 75, 25, 0) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvents") GUICtrlSetOnEvent($Button1, "_GuiCtrlEvents") GUICtrlSetOnEvent($Button2, "_GuiCtrlEvents") GUICtrlSetOnEvent($Button3, "_GuiCtrlEvents") While 1 Sleep(100) WEnd Func _GuiEvents() Switch @GUI_CtrlId Case $GUI_EVENT_CLOSE If @GUI_WinHandle = $Form1 Then Exit EndIf EndSwitch EndFunc ;==>_GuiEvents Func _GuiCtrlEvents() Select Case @GUI_CtrlId = $Button1 ;~ MsgBox(0,"",@GUI_CtrlId) If Not $hTimer Then _NewTimer() Case @GUI_CtrlId = $Button2 _NewTimer(1) ;~ MsgBox(0,"",@GUI_CtrlId) Case @GUI_CtrlId = $Button3 MsgBox(0, "", @GUI_CtrlId) EndSelect EndFunc ;==>_GuiCtrlEvents Func _NewTimer($sMode = 0) If $sMode Then If Not $hTimer Then Return _Timer_KillTimer($Form1, $hTimer) $hTimer = 0 MsgBox(0, "Interupted", "Button2 stopped timmer") $iTimerValue = 0 GUICtrlSetData($Label1, $iTimerValue) Return EndIf $hTimer = _Timer_SetTimer($Form1, 1000, "_Updatetime") EndFunc ;==>_NewTimer Func _Updatetime($hWnd, $Msg, $iIDTimer, $dwTime) $iTimerValue += 1 GUICtrlSetData($Label1, $iTimerValue) If $iTimerValue >= $iTimerDuration Then _Timer_KillTimer($Form1, $hTimer) $hTimer = 0 MsgBox(0, "Done", "Timmer reached Duration") $iTimerValue = 0 GUICtrlSetData($Label1, $iTimerValue) EndIf EndFunc ;==>_Updatetime GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF. Link to comment Share on other sites More sharing options...
MrFlibble Posted June 18, 2010 Author Share Posted June 18, 2010 MrFlibble, I would do it by using an Adlib function like this: expandcollapse popup#include <GUIConstantsEx.au3> Opt("GUIOnEventMode", 1) $hGUI = GUICreate("Test", 500, 500) GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvents") $hLabel = GUICtrlCreateLabel("0", 10, 10, 50, 50) GUICtrlSetFont(-1, 24) $hButton_1 = GUICtrlCreateButton("1", 10, 100, 80, 30) GUICtrlSetOnEvent($hButton_1, "_GuiCtrlEvents") $hButton_2 = GUICtrlCreateButton("2", 110, 100, 80, 30) GUICtrlSetOnEvent($hButton_2, "_GuiCtrlEvents") $hButton_3 = GUICtrlCreateButton("3", 210, 100, 80, 30) GUICtrlSetOnEvent($hButton_3, "_GuiCtrlEvents") GUISetState() While 1 Sleep(10) WEnd Func _GuiEvents() Switch @GUI_CtrlId Case $GUI_EVENT_CLOSE If @GUI_WinHandle = $hGUI Then Exit EndSwitch EndFunc Func _GuiCtrlEvents() Switch @GUI_CtrlId Case $hButton_1 ConsoleWrite(@GUI_CtrlId & @CRLF) ; Start the Adlib function AdlibRegister("_CheckIt", 1000) Case $hButton_2 ConsoleWrite(@GUI_CtrlId & @CRLF) Case $hButton_3 ConsoleWrite(@GUI_CtrlId & @CRLF) EndSwitch EndFunc ;==>_GuiCtrlEvents Func _CheckIt() ; Increase the count GUICtrlSetData($hLabel, GUICtrlRead($hLabel) + 1) ; Check if another control pressed or timer expired If @GUI_CtrlId <> $hButton_1 Or GUICtrlRead($hLabel) = 10 Then ; Cancel Adlib function AdlibUnRegister("_CheckIt") ; Reset counter GUICtrlSetData($hLabel, 0) EndIf EndFunc Please ask if anything is unclear. M23 Thanks M23 for the help. This might be what I need but not sure if again I'm going the wrong way about it, but $button1 will call a function, do "some stuff" then i need it to pause for a set amount of time unless another button is pressed and then carry on with the rest of the function. I amended the exmaple you kindly provided above. Not sure if i'm doing something wrong but in my example below I need it to play the tada wav, wait 10 seconds then play the next tada, unless another button is pressed. Mine is playing, Adlib is starting the counter but the tada following Adlib plays immediately, and not wating for the time to reach 10. expandcollapse popup#include <GUIConstantsEx.au3> Opt("GUIOnEventMode", 1) Global $i_Start = 1 $hGUI = GUICreate("Test", 500, 500) GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvents") $hLabel = GUICtrlCreateLabel("0", 10, 10, 50, 50) GUICtrlSetFont(-1, 24) $hButton_1 = GUICtrlCreateButton("1", 10, 100, 80, 30) GUICtrlSetOnEvent($hButton_1, "_GuiCtrlEvents") $hButton_2 = GUICtrlCreateButton("2", 110, 100, 80, 30) GUICtrlSetOnEvent($hButton_2, "_GuiCtrlEvents") $hButton_3 = GUICtrlCreateButton("3", 210, 100, 80, 30) GUICtrlSetOnEvent($hButton_3, "_GuiCtrlEvents") GUISetState() While 1 Sleep(10) WEnd Func _GuiEvents() Switch @GUI_CtrlId Case $GUI_EVENT_CLOSE If @GUI_WinHandle = $hGUI Then Exit EndSwitch EndFunc Func _GuiCtrlEvents() Switch @GUI_CtrlId Case $hButton_1 ConsoleWrite(@GUI_CtrlId & @CRLF) DoSomething() Case $hButton_2 ConsoleWrite(@GUI_CtrlId & @CRLF) Case $hButton_3 ConsoleWrite(@GUI_CtrlId & @CRLF) EndSwitch EndFunc ;==>_GuiCtrlEvents Func DoSomething() SoundPlay(@WindowsDir & "\media\tada.wav",1) AdlibRegister("_CheckIt", 1000) SoundPlay(@WindowsDir & "\media\tada.wav",1) EndFunc Func _CheckIt() ; Increase the count $i_Start = $i_Start + 1 GUICtrlSetData($hLabel, $i_Start) ; Check if another control pressed or timer expired If @GUI_CtrlId <> $hButton_1 Or $i_Start = 10 Then ; Cancel Adlib function AdlibUnRegister("_CheckIt") ; Reset counter GUICtrlSetData($hLabel, 0) EndIf EndFunc Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 18, 2010 Moderators Share Posted June 18, 2010 MrFlibble,My "goalpost moving" detectors have just gone off! But they are very finely tuned at the moment because of the World Cup, so you are forgiven....this time! This should do what you now want:expandcollapse popup#include <GUIConstantsEx.au3> ; Declare Global variable - visible to all functions Global $iBegin Opt("GUIOnEventMode", 1) $hGUI = GUICreate("Test", 500, 500) GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvents") $hLabel = GUICtrlCreateLabel("0", 10, 10, 50, 50) GUICtrlSetFont(-1, 24) $hButton_1 = GUICtrlCreateButton("1", 10, 100, 80, 30) GUICtrlSetOnEvent($hButton_1, "_GuiCtrlEvents") $hButton_2 = GUICtrlCreateButton("2", 110, 100, 80, 30) GUICtrlSetOnEvent($hButton_2, "_GuiCtrlEvents") $hButton_3 = GUICtrlCreateButton("3", 210, 100, 80, 30) GUICtrlSetOnEvent($hButton_3, "_GuiCtrlEvents") GUISetState() While 1 Sleep(10) WEnd Func _GuiEvents() Switch @GUI_CtrlId Case $GUI_EVENT_CLOSE If @GUI_WinHandle = $hGUI Then Exit EndSwitch EndFunc Func _GuiCtrlEvents() Switch @GUI_CtrlId Case $hButton_1 ConsoleWrite(@GUI_CtrlId & @CRLF) ; Get time stamp $iBegin = TimerInit() ; Start Adlib function AdlibRegister("_DoSomething") ; Play initial sound SoundPlay(@WindowsDir & "\media\tada.wav",1) Case $hButton_2 ConsoleWrite(@GUI_CtrlId & @CRLF) Case $hButton_3 ConsoleWrite(@GUI_CtrlId & @CRLF) EndSwitch EndFunc ;==>_GuiCtrlEvents Func _DoSomething() ; Show secs GUICtrlSetData($hLabel, Int(TimerDiff($iBegin) / 1000)) ; If at 10 secs If Int(TimerDiff($iBegin) / 1000) > 9 Then ; Play sound SoundPlay(@WindowsDir & "\media\tada.wav",1) ; Reset time stamp $iBegin = TimerInit() EndIf ; Check if another control pressed If @GUI_CtrlId <> $hButton_1 Then ; Cancel Adlib function AdlibUnRegister("_DoSomething") ; Reset counter GUICtrlSetData($hLabel, 0) EndIf EndFuncAre we getting there? M23P.S. When you reply please use the "Add Reply" button at the top and bottom of the page rather then the "Reply" button in the post itself. That way you do not get the contents of the previous post quoted in your reply and the whole thread becomes easier to read. 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...
MrFlibble Posted June 19, 2010 Author Share Posted June 19, 2010 Sorry M23 it was more a case of not explaining myself properly. What I really should have said at the start, was that I want a sleep that the user can interrupt. My first example was just showing my way of how I was trying to come out a loop. I'll explain exactly what currently happens. A button is pressed which calls a function and text is displayed, then goes to sleep for a few seconds it then displays different text. The gui has several other buttons that call different functions. Obviously when the sleep is called the gui is unresponsive, which i dont want. My first post was my testing of a way of having a timed loop and coming out of it if any other button was pressed. i'll have a good play with AdlibRegister as it definitely looks like the way I should be going. Hope your enjoying the world cup, think it would be better if the goal posts were widened some teams may actually score then! Thanks for all your help and sorry for my misleading posts. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 19, 2010 Moderators Share Posted June 19, 2010 MrFlibble,No problem.It is just that what you do in a function often determines how you can best interrupt it. Some methods are better than others depending on the situation.Do post again if you run into trouble. M23P.S.Hope your enjoying the world cupAs I am English, what do you think! 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...
MrFlibble Posted June 20, 2010 Author Share Posted June 20, 2010 I still cant seem to resolve this. I've tried lots of different ways of using Adlibregister but it is not recognising another button has been pressed until it has come out of the loop. Anyone got any ideas? My last effort. I feel like i'm doing something real stupid and missing the obvious but i cant figure it out. expandcollapse popup#include <GUIConstantsEx.au3> ; Declare Global variable - visible to all functions Global $i_ExitCounter = 0 Global $iBegin Opt("GUIOnEventMode", 1) $hGUI = GUICreate("Test", 500, 500) GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvents") $hLabel = GUICtrlCreateLabel("0", 10, 10, 50, 50) GUICtrlSetFont(-1, 24) $hButton_1 = GUICtrlCreateButton("1", 10, 100, 80, 30) GUICtrlSetOnEvent($hButton_1, "_GuiCtrlEvents") $hButton_2 = GUICtrlCreateButton("2", 110, 100, 80, 30) GUICtrlSetOnEvent($hButton_2, "_GuiCtrlEvents") $hButton_3 = GUICtrlCreateButton("3", 210, 100, 80, 30) GUICtrlSetOnEvent($hButton_3, "_GuiCtrlEvents") GUISetState() While 1 Sleep(10) WEnd Func _GuiEvents() Switch @GUI_CtrlId Case $GUI_EVENT_CLOSE If @GUI_WinHandle = $hGUI Then Exit EndSwitch EndFunc Func _GuiCtrlEvents() Switch @GUI_CtrlId Case $hButton_1 ConsoleWrite(@GUI_CtrlId & @CRLF) _test() Case $hButton_2 ConsoleWrite(@GUI_CtrlId & @CRLF) Case $hButton_3 ConsoleWrite(@GUI_CtrlId & @CRLF) EndSwitch EndFunc ;==>_GuiCtrlEvents Func _Test() GUICtrlSetData($hLabel, "Test") Sleep(1000) GUICtrlSetData($hLabel, "") Sleep(1000) SoundPlay(@WindowsDir & "\media\tada.wav",1) GUICtrlSetData($hLabel, "Timer") $iBegin = TimerInit() AdlibRegister("_DoSomething") Do Sleep(50) GUICtrlSetData($hLabel, Int(TimerDiff($iBegin) / 1000)) Until $i_ExitCounter = 1 AdlibUnRegister("_DoSomething") GUICtrlSetData($hLabel, 0) SoundPlay(@WindowsDir & "\media\tada.wav",1) EndFunc Func _DoSomething() ConsoleWrite(@GUI_CtrlId) If Int(TimerDiff($iBegin) / 1000) > 9 Then ConsoleWrite(Int(TimerDiff($iBegin) / 1000)&@CRLF) ConsoleWrite("Timer Ended") $i_ExitCounter = 1 EndIf If @GUI_CtrlId <> $hButton_1 Then ConsoleWrite(@GUI_CtrlId) $i_ExitCounter = 1 EndIf EndFunc Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 20, 2010 Moderators Share Posted June 20, 2010 MrFlibble,Another attempt from me to match what you seem to want. You do whatever it is you need to do in Func _DoSomething() - at present we just play "tada". The function will run when the registered Adlib function determines that a certain time has elapsed - although you do have to run it just the once as the loop starts because the Adlib function will obviously not fire until the first timed period has passed. Pressing one of the other buttons unregisters the Adlib function and so prevents Func _DoSomething() being called again.expandcollapse popup#include <GUIConstantsEx.au3> ; Declare Global variable - visible to all functions Global $iBegin Opt("GUIOnEventMode", 1) $hGUI = GUICreate("Test", 500, 500) GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvents") $hLabel = GUICtrlCreateLabel("0", 10, 10, 50, 50) GUICtrlSetFont(-1, 24) $hButton_1 = GUICtrlCreateButton("1", 10, 100, 80, 30) GUICtrlSetOnEvent($hButton_1, "_GuiCtrlEvents") $hButton_2 = GUICtrlCreateButton("2", 110, 100, 80, 30) GUICtrlSetOnEvent($hButton_2, "_GuiCtrlEvents") $hButton_3 = GUICtrlCreateButton("3", 210, 100, 80, 30) GUICtrlSetOnEvent($hButton_3, "_GuiCtrlEvents") GUISetState() While 1 Sleep(10) WEnd Func _GuiEvents() Switch @GUI_CtrlId Case $GUI_EVENT_CLOSE If @GUI_WinHandle = $hGUI Then Exit EndSwitch EndFunc ;==>_GuiEvents Func _GuiCtrlEvents() Switch @GUI_CtrlId Case $hButton_1 ConsoleWrite(@GUI_CtrlId & @CRLF) ; Start the loop to run Func _DoSomething every 10 secs _test() Case $hButton_2 ConsoleWrite(@GUI_CtrlId & " Unregistering Adlib" & @CRLF) ; Stop the Adlib function running and so stop the _DoSomething loop AdlibUnRegister("_Timer") GUICtrlSetData($hLabel, 0) Case $hButton_3 ConsoleWrite(@GUI_CtrlId & " Unregistering Adlib" & @CRLF) ; Stop the Adlib function running and so stop the _DoSomething loop AdlibUnRegister("_Timer") GUICtrlSetData($hLabel, 0) EndSwitch EndFunc ;==>_GuiCtrlEvents Func _Test() ConsoleWrite(@GUI_CtrlId & " Running _Test" & @CRLF) ; Run _DoSomething once because Adlib will not run for another 10 secs _DoSomething() ; Register the Adlib function - it will run every 250 ms checking if the 10 secs are up ConsoleWrite("Registering Adlib" & @CRLF) $iBegin = TimerInit() AdlibRegister("_Timer") ConsoleWrite("Exiting _Test" & @CRLF) EndFunc ;==>_Test Func _DoSomething() ; Here is where you do whatever it is you want to do every 10 secs SoundPlay(@WindowsDir & "\media\tada.wav", 1) EndFunc ;==>_DoSomething Func _Timer() ; Show timer running GUICtrlSetData($hLabel, Int(TimerDiff($iBegin) / 1000)) ; If timer reaches 10 secs If Int(TimerDiff($iBegin) / 1000) > 9 Then ConsoleWrite("Timer Ended" & @CRLF) ; Do your "every 10 sec" thing by running Func _DoSomething _DoSomething() ; Restart timer ConsoleWrite("Timer Restarting" & @CRLF) $iBegin = TimerInit() EndIf EndFunc ;==>_TimerYou now have a loop which fires continually every 10 seconds until you press another button at which point it ceases.Is this what you want? 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...
MrFlibble Posted June 21, 2010 Author Share Posted June 21, 2010 Another attempt from me to match what you seem to want. First of all thanks for your help with this M23. I really appreciate it and I am trying to figure this out and not just throw examples at you. Is this what you want? Unfortunately not. I haven’t posted the script that I’m trying to incorporate this into as it is 1800 lines long. The example scripts were purely examples by myself to work out how to exit a timed loop, but I feel that they have been more of a distraction and I have led you down the wrong path. The following is a smaller version of what I’m doing with the sleeps left in. If you run it you can see the gui is obviously totally unresponsive during the sleep. I was hoping to be able to replace the sleep with an alternative timed loop that can be interrupted if need be. Script runs line n in function “A” then enters a timed loop with variable time. If user intervenes, exit loop and do what user wants e.g. Leave function “A” and start function “B” If no user intervention, then when the timer reaches set limit, run line n+1 in function “A”. What I have found with AdLibRegister that it moves on to line n+ 1 straight away, but I need it to pause at that line. The commented out bit are where i tried to add the AdLib in. expandcollapse popup#include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Opt("GUIOnEventMode", 1) ;Not that it matters but just for clarification these are read from a file. After an answer is selected it automaticalls goes onto the next question. Global $Answer = "Thanks" Global $AnotherWord = "Diolch yn fawr" ;Alternative wrong answers again read from a file Global $s_ALternate = "England,Football,Supporter,that,is,unlucky,could,be,worse,you,could,be,a,welsh,rugby,supporter" Global $i_RealAnswer Global $i_Start Global $played2 = False Global $iBegin #Region ### START Koda GUI section ### Form= $Form1_1 = GUICreate("Form1", 626, 446, 192, 124) $Label1 = GUICtrlCreateLabel("Label1", 104, 16, 372, 81, $SS_CENTER) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") $Answer1 = GUICtrlCreateButton("", 80, 112, 179, 113, $WS_GROUP) GUICtrlSetOnEvent($Answer1, "_GuiCtrlEvents") $Answer2 = GUICtrlCreateButton("", 311, 113, 179, 113, $WS_GROUP) GUICtrlSetOnEvent($Answer2, "_GuiCtrlEvents") $Answer3 = GUICtrlCreateButton("", 84, 254, 179, 113, $WS_GROUP) GUICtrlSetOnEvent($Answer3, "_GuiCtrlEvents") $Answer4 = GUICtrlCreateButton("", 309, 258, 179, 113, $WS_GROUP) GUICtrlSetOnEvent($Answer4, "_GuiCtrlEvents") $Button5 = GUICtrlCreateButton("Quit", 528, 400, 75, 25, $WS_GROUP) GUICtrlSetOnEvent($Button5, "_GuiCtrlEvents") $Button6 = GUICtrlCreateButton("Button6", 432, 400, 75, 25, $WS_GROUP) GUICtrlSetOnEvent($Button6, "_GuiCtrlEvents") $Button7 = GUICtrlCreateButton("Button7", 184, 400, 75, 25, $WS_GROUP) GUICtrlSetOnEvent($Button7, "_GuiCtrlEvents") $Button8 = GUICtrlCreateButton("Start", 288, 400, 75, 25, $WS_GROUP) GUICtrlSetOnEvent($Button8, "_GuiCtrlEvents") $Input1 = GUICtrlCreateInput("", 16, 392, 41, 37) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") $Input2 = GUICtrlCreateInput("", 71, 395, 41, 37) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvents") Dim $a_BtnHwnd[4] = [$Answer1, $Answer2, $Answer3, $Answer4] While 1 Sleep(10) WEnd Func _GuiEvents() Switch @GUI_CtrlId Case $GUI_EVENT_CLOSE If @GUI_WinHandle = $Form1_1 Then Exit EndSwitch EndFunc ;==>_GuiEvents Func _GuiCtrlEvents() Switch @GUI_CtrlId Case $Answer1 _CheckAnswer() Case $Answer2 _CheckAnswer() Case $Answer3 _CheckAnswer() Case $Answer4 _CheckAnswer() Case $Button5 Exit Case $Button6 ConsoleWrite("Function 3 called") Case $Button7 ConsoleWrite("Function 4 called") Case $Button8 _start() EndSwitch EndFunc ;==>_GuiCtrlEvents Func _start() $a_data = StringSplit($s_ALternate,",") Do $randomWord1 = $a_data[Random(1,$a_data[0])] Until $randomWord1 <> $Answer Do $randomWord2 = $a_data[Random(1,$a_data[0])] Until $randomWord2 <> $randomWord1 And $randomWord2 <> $Answer Do $randomWord3 = $a_data[Random(1,$a_data[0])] Until $randomWord3 <> $randomWord1 And $randomWord3 <> $randomWord2 And $randomWord3 <> $Answer $i_BtnId1 = Random(0,3,1) Do $i_BtnId2 = Random(0,3,1) Until $i_BtnId2 <> $i_BtnId1 Do $i_BtnId3 = Random(0,3,1) Until $i_BtnId3 <> $i_BtnId1 And $i_BtnId3 <> $i_BtnId2 Do $i_BtnId4 = Random(0,3,1) Until $i_BtnId4 <> $i_BtnId1 And $i_BtnId4 <> $i_BtnId2 And $i_BtnId4 <> $i_BtnId3 $i_RealAnswer = $a_BtnHwnd[$i_BtnId4] GUICtrlSetData($Label1,$Answer) GUICtrlSetState($Label1,$GUI_SHOW) SoundPlay(@WindowsDir & "\media\tada.wav",1) Sleep(10000);this is what i would like to replace with a timed loop that a user can interrupt. the length of timer is also variable set at an earlier time by the user GUICtrlSetData($Label1,$AnotherWord) SoundPlay(@WindowsDir & "\media\tada.wav",1) Sleep(10000);again to be replaced #comments-start $i_Start =1 _DoSomething() $iBegin = TimerInit() AdlibRegister("_Timer") Do Sleep(50) Until $played2 = True #comments-end $x = GUICtrlSetData($a_BtnHwnd[$i_BtnId1] ,$randomWord1) $x =GUICtrlSetData($a_BtnHwnd[$i_BtnId2] ,$randomWord2) $x =GUICtrlSetData($a_BtnHwnd[$i_BtnId3] ,$randomWord3) $x =GUICtrlSetData($a_BtnHwnd[$i_BtnId4] ,$Answer) EndFunc Func _CheckAnswer() If @GUI_CtrlId = $i_RealAnswer Then MsgBox(0,"","Congrats, now we automatically go to another question") Else MsgBox(0,"","unlcuky, now we automatically go to another question") EndIf EndFunc #comments-start Func _DoSomething() If $i_Start =1 Then GUICtrlSetData($Label1,$Answer) GUICtrlSetState($Label1,$GUI_SHOW) SoundPlay(@WindowsDir & "\media\tada.wav", 1) Else GUICtrlSetData($Label1,$AnotherWord) SoundPlay(@WindowsDir & "\media\tada.wav",1);here you would hear the word being said not tada $i_Start = 1 $played2 = True ConsoleWrite($played2 & @CRLF) AdlibUnRegister("_Timer") EndIf $i_Start = 2 EndFunc ;==>_DoSomething Func _Timer() ; Show timer running ConsoleWrite( Int(TimerDiff($iBegin) / 1000)&@CRLF) ; If timer reaches 10 secs If Int(TimerDiff($iBegin) / 1000) > 9 Then ConsoleWrite("Timer Ended" & @CRLF) ; Do your "every 10 sec" thing by running Func _DoSomething _DoSomething() ; Restart timer ConsoleWrite("Timer Restarting" & @CRLF) $iBegin = TimerInit() EndIf EndFunc ;==>_Timer #comments-end I dont understand why the DoSomething didn’t work in the second example. Dosomething() is being called during the loop so why doesn’t it see that the last button pressed is no longer button1 until it exits the loop? Func _DoSomething() ConsoleWrite(@GUI_CtrlId) If Int(TimerDiff($iBegin) / 1000) > 9 Then ConsoleWrite(Int(TimerDiff($iBegin) / 1000)&@CRLF) ConsoleWrite("Timer Ended") $i_ExitCounter = 1 EndIf If @GUI_CtrlId <> $hButton_1 Then ConsoleWrite(@GUI_CtrlId) $i_ExitCounter = 1 EndIf EndFunc Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 21, 2010 Moderators Share Posted June 21, 2010 (edited) MrFlibble, First of all thanks for your help with this M23. I really appreciate it and I am trying to figure this out and not just throw examples at you.Delighted to assist - that is why I come here! At last we get to the real problem! If I change your script to GetMessage mode rather than OnEvent mode, I can get this to work rather easily. I realise that you will have to make other changes in your 1800 lines of code - but the manner in which you were using the OnEvent mode meant that you were essentially using GetMessage mode anyway! What I have done is used a small GUIGetMsg loop in place of your Sleep statements - that way the buttons you check during this loop remain active and the function can be curtailed if one of them is pressed. Then when you return to the main loop, a global variable tells you which button interrupted the "sleep" and lets you activate the new required function. Take a look and tell me what you think - I am quite impressed with it (but then I would say that anyway! ): expandcollapse popup#include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> ;Opt("GUIOnEventMode", 1) ;Not that it matters but just for clarification these are read from a file. After an answer is selected it automaticalls goes onto the next question. Global $Answer = "Thanks" Global $AnotherWord = "Diolch yn fawr" ;Alternative wrong answers again read from a file Global $s_ALternate = "England,Football,Supporter,that,is,unlucky,could,be,worse,you,could,be,a,welsh,rugby,supporter" Global $i_RealAnswer Global $i_Start Global $played2 = False Global $iBegin #Region ### START Koda GUI section ### Form= $Form1_1 = GUICreate("Form1", 626, 446, 192, 124) $Label1 = GUICtrlCreateLabel("Label1", 104, 16, 372, 81, $SS_CENTER) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") $Answer1 = GUICtrlCreateButton("", 80, 112, 179, 113, $WS_GROUP) ;GUICtrlSetOnEvent($Answer1, "_GuiCtrlEvents") $Answer2 = GUICtrlCreateButton("", 311, 113, 179, 113, $WS_GROUP) ;GUICtrlSetOnEvent($Answer2, "_GuiCtrlEvents") $Answer3 = GUICtrlCreateButton("", 84, 254, 179, 113, $WS_GROUP) ;GUICtrlSetOnEvent($Answer3, "_GuiCtrlEvents") $Answer4 = GUICtrlCreateButton("", 309, 258, 179, 113, $WS_GROUP) ;GUICtrlSetOnEvent($Answer4, "_GuiCtrlEvents") $Button5 = GUICtrlCreateButton("Quit", 528, 400, 75, 25, $WS_GROUP) ;GUICtrlSetOnEvent($Button5, "_GuiCtrlEvents") $Button6 = GUICtrlCreateButton("Button6", 432, 400, 75, 25, $WS_GROUP) ;GUICtrlSetOnEvent($Button6, "_GuiCtrlEvents") $Button7 = GUICtrlCreateButton("Button7", 184, 400, 75, 25, $WS_GROUP) ;GUICtrlSetOnEvent($Button7, "_GuiCtrlEvents") $Button8 = GUICtrlCreateButton("Start", 288, 400, 75, 25, $WS_GROUP) ;GUICtrlSetOnEvent($Button8, "_GuiCtrlEvents") $Input1 = GUICtrlCreateInput("", 16, 392, 41, 37) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") $Input2 = GUICtrlCreateInput("", 71, 395, 41, 37) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### ;GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvents") Dim $a_BtnHwnd[4] = [$Answer1, $Answer2, $Answer3, $Answer4] ; Declare our flag as "not" an array - it will be set to an array when we interrupt the "sleep" Global $aInterrupt = 0 While 1 $aMsg = GUIGetMsg(1) ; Were we interrupted - is the flag is an array? If so, set the message array to match the interrupt message array If IsArray($aInterrupt) Then $aMsg = $aInterrupt $aInterrupt = 0 EndIf ; Check which GUI has sent message Switch $aMsg[1] Case $Form1_1 ; Check message Switch $aMsg[0] Case $GUI_EVENT_CLOSE, $Button5 Exit Case $Answer1 _CheckAnswer() Case $Answer2 _CheckAnswer() Case $Answer3 _CheckAnswer() Case $Answer4 _CheckAnswer() Case $Button6 ConsoleWrite("Function 3 called" & @CRLF) Case $Button7 ConsoleWrite("Function 4 called" & @CRLF) Case $Button8 _start() EndSwitch EndSwitch WEnd Func _start() $a_data = StringSplit($s_ALternate, ",") Do $randomWord1 = $a_data[Random(1, $a_data[0])] Until $randomWord1 <> $Answer Do $randomWord2 = $a_data[Random(1, $a_data[0])] Until $randomWord2 <> $randomWord1 And $randomWord2 <> $Answer Do $randomWord3 = $a_data[Random(1, $a_data[0])] Until $randomWord3 <> $randomWord1 And $randomWord3 <> $randomWord2 And $randomWord3 <> $Answer $i_BtnId1 = Random(0, 3, 1) Do $i_BtnId2 = Random(0, 3, 1) Until $i_BtnId2 <> $i_BtnId1 Do $i_BtnId3 = Random(0, 3, 1) Until $i_BtnId3 <> $i_BtnId1 And $i_BtnId3 <> $i_BtnId2 Do $i_BtnId4 = Random(0, 3, 1) Until $i_BtnId4 <> $i_BtnId1 And $i_BtnId4 <> $i_BtnId2 And $i_BtnId4 <> $i_BtnId3 $i_RealAnswer = $a_BtnHwnd[$i_BtnId4] GUICtrlSetData($Label1, $Answer) GUICtrlSetState($Label1, $GUI_SHOW) SoundPlay(@WindowsDir & "\media\tada.wav", 1) ; If we were interrupted, return now If TimedSleep() = 1 Then Return GUICtrlSetData($Label1, $AnotherWord) SoundPlay(@WindowsDir & "\media\tada.wav", 1) ; If we were interrupted, return now If TimedSleep() = 1 Then Return #comments-start $i_Start =1 _DoSomething() $iBegin = TimerInit() AdlibRegister("_Timer") Do Sleep(50) Until $played2 = True #comments-end $x = GUICtrlSetData($a_BtnHwnd[$i_BtnId1], $randomWord1) $x = GUICtrlSetData($a_BtnHwnd[$i_BtnId2], $randomWord2) $x = GUICtrlSetData($a_BtnHwnd[$i_BtnId3], $randomWord3) $x = GUICtrlSetData($a_BtnHwnd[$i_BtnId4], $Answer) EndFunc ;==>_start Func TimedSleep() ConsoleWrite("In TimedSleep" & @CRLF) $iBegin = TimerInit() ; Loop for 10 secs - just adjust the timing by usign a variable rather then 10000 While TimerDiff($iBegin) < 10000 ; check for a message - I am ssumimg that it is only the man window that will be sending at this time $iMsg = GUIGetMsg() Switch $iMsg Case $GUI_EVENT_CLOSE, $Button5, $Button6, $Button7 ; Declare aur flag as an array Global $aInterrupt[2] $aInterrupt[0] = $iMsg ; Message $aInterrupt[1] = $Form1_1 ; GUI Return 1 ; Return 1 to show interrupt EndSwitch WEnd Return 0 ; Return 0 to show no interrupt EndFunc Func _CheckAnswer() If @GUI_CtrlId = $i_RealAnswer Then MsgBox(0, "", "Congrats, now we automatically go to another question") Else MsgBox(0, "", "unlcuky, now we automatically go to another question") EndIf EndFunc ;==>_CheckAnswer Are we somewhere close this time? M23 P.S. I noticed that you were checking the GUI handle in your original code - I have therefore used the "advanced" parameter for the GUIGetMsg calls to detect the GUI sending the message. If you are NOT using multiple GUIs, we can simplify the code further as there is then no need to set an array each time we interrupt the "sleep". Just let me know which is the case. Edit: Typnig! Edited June 21, 2010 by Melba23 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...
MrFlibble Posted June 21, 2010 Author Share Posted June 21, 2010 Are we somewhere close this time?What can I say, dude thank you very much that’s exactly what I’m after. Sorry to have led you up the garden path with the other examples. I mistakenly thought it would be easier to post a little test gui with an example...it would have been a lot easier to have got straight to the point from the outset.There are four other Gui’s, but while this one is in use no others exist so I don’t see their being a problem changing from OnEvent to GetMessage. Well I hope not anyway.Take a look and tell me what you think - I am quite impressed with it (but then I woudl say that anyway!Well I’m going to be biased as I’ve been banging my head trying to figure this out, but honestly I think you deserve Legend status. I really am impressed with that..thankyou. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 21, 2010 Moderators Share Posted June 21, 2010 MrFlibble, Delighted we got there! It is sometimes better to expose the actual problem rather than try to simplify it and lose the essence of what it is - although I do not know how many responses you would have had with an 1800 line code posting! If you are only using the one GUI, then you can simplify the code as shown below: ; Declare our flag - it will be set to a Msg when we interrupt the "sleep" Global $iInterrupt = 0 While 1 $aMsg = GUIGetMsg(1) ; Were we interrupted - is the flag set? If so, set the message to match the interrupt message If $iInterrupt Then $aMsg[0] = $iInterrupt $aMsg[1] = $Form1_1 ; we know this will be the case $iInterrupt = 0 EndIf ; ---------- Func TimedSleep() ConsoleWrite("In TimedSleep" & @CRLF) $iBegin = TimerInit() ; Loop for 10 secs - just adjust the timing by usign a variable rather then 10000 While TimerDiff($iBegin) < 10000 ; check for a message $iMsg = GUIGetMsg() Switch $iMsg Case $GUI_EVENT_CLOSE, $Button5, $Button6, $Button7 ; Set our flag $iInterrupt = $iMsg ; Message Return 1 ; Return 1 to show interrupt EndSwitch WEnd Return 0 ; Return 0 to show no interrupt EndFunc I hope the rest of the coding goes well - you know where we are if you need any more help! 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