ToddBrownshoes Posted January 15, 2013 Posted January 15, 2013 (edited) What I'd like to have is a function that will allow me to count down (by whole numbers, if possible, not decimels) seconds and when it's finished counting down, continue with the original loop, but can also be terminated by pressing a GUI button. While 1 Function_1 Function_2 Randapp (10,20) Function_3 WEnd I basically would like to be able to loop this until terminated, but be able to terminate the function Randapp early and continue the loop. This terminates the function and then continues the loop, but I cannot terminate the function early by pressing the GUI button: expandcollapse popupFunc Randapp ($minrapp,$maxrapp) Local $rapp, $Button1, $msg $Form = GUICreate("Timer", 300, 150, 200, 125) $Label = GUICtrlCreateLabel("", 128, 32, 76, 28) $Button1 = GUICtrlCreateButton ("Cancel", 20, 60) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") GUISetState(@SW_SHOW) $rapp = Random ($minratk,$maxratk) $msg = GUIGetMsg () While $rapp > 0 $msg = GUIGetMsg () Switch $rapp Case 1 To 99999 $rapp = $rapp - 1 GUICtrlSetData($Label, $ratk) Sleep (1000) If $msg = $Button1 Then $rapp = 0 Sleep (500) EndIf Case Else Sleep (100) GuiDelete () ExitLoop EndSwitch WEnd Sleep(1000) EndFunc I've also tried a For-To-Step-Next loop and that was also non-interruptible. I've tried ExitLoop instead of setting the $rapp variable as 0 as well with the same results. Edited January 15, 2013 by ToddBrownshoes
kylomas Posted January 15, 2013 Posted January 15, 2013 Toddbrownshoes, Your code is not only not runnable but need heavy reformatting to make any sense out of it at all. I hope it was just an editor problem. Regardless, this example will do what I think you are asking. The code can be streamlined but that will be up to you. expandcollapse popup; *** Start added by AutoIt3Wrapper *** #include <GUIConstantsEx.au3> ; *** End added by AutoIt3Wrapper *** #AutoIt3Wrapper_Add_Constants=n local $gui010 = guicreate('Main Gui') local $aSize = WinGetClientSize($gui010) local $btn010 = guictrlcreatebutton('My Countdown Button',0,$aSize[1]-30,$aSize[0]) guisetstate() local $looptime while 1 switch guigetmsg() case $gui_event_close Exit case $btn010 $looptime = random(3,10,1) Randapp(1,$looptime) EndSwitch WEnd Func Randapp ($minrapp,$maxrapp) local $gui020 = guicreate('Countdown Timer',100,100) local $btn020 = guictrlcreatebutton('Stop Timer',0,50,100,20) local $lbl010 = guictrlcreatelabel('',40,20,20,20) GUISetState() local $save_sec while 1 switch guigetmsg() case $gui_event_close guidelete($gui020) Return case $btn020 guidelete($gui020) Return EndSwitch if $save_sec <> @sec then guictrlsetdata($lbl010,$maxrapp) $maxrapp -= 1 if $maxrapp = 0 then guidelete($gui020) Return endif $save_sec = @SEC endif wend EndFunc kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
ToddBrownshoes Posted January 15, 2013 Author Posted January 15, 2013 (edited) Please excuse the inelegance of my code; I have no coding background and have only been using Auto-it for a little while. Thank you, that worked quite well. Also, what do you think caused it to be uninterruptible? Was it using the Sleep () function? Edited January 15, 2013 by ToddBrownshoes
kylomas Posted January 15, 2013 Posted January 15, 2013 Todd,Inelegance is one thing, we all have our own style. Not runnable is quite another. Let me encourage you to download and use the full editor that comes with Autoit. It has many tools to aid in coding.The code I posted is just a skeleton of one way to do your task. Break the code down into pieces to understand how/why it works. You will find the support resources on this forum excellent, provided that you are interested in learning.kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
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