SorryButImaNewbie Posted December 17, 2014 Share Posted December 17, 2014 (edited) Hello! I already posted a question like this, but I think that I wrote it down the wrong way, and the questions in it was of a too broad spectrum, too general. I try to create an interrupt function, I run my functions OnEventMode, from the main code, using flags to check if a functions should run or not. I'm pretty sure that the wiki (https://www.autoitscript.com/wiki/Interrupting_a_running_function) has it right, I found Melba queting it exactly in an older thread () , but I still dont understand how do I initiate the interrupt it self, or how does it stop the function exactly. The part I dont understand: (as posted by Melba23) expandcollapse popup#include <GUIConstantsEx.au3> ; Declare a flag Global $fRunOne = False Opt("GUIOnEventMode", 1) $hGUI = GUICreate("Test", 500, 500) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit") $hButton_1 = GUICtrlCreateButton("Func One", 10, 10, 80, 30) GUICtrlSetOnEvent($hButton_1, "_Func_1") $hButton_2 = GUICtrlCreateButton("Func Two", 10, 50, 80, 30) GUICtrlSetOnEvent($hButton_2, "_Func_2") GUISetState() While 1 Sleep(10) ; Check if the flag has been set by the OnEvent function If $fRunOne Then ; Now start the "real" function from within the main code _Func_1_Run() EndIf WEnd Func _Func_1() ; Set the flag within the OnEvent function $fRunOne = True EndFunc ;==>_Func_1 Func _Func_1_Run() For $i = 1 To 20 ConsoleWrite("-Func 1 Running" & @CRLF) If $fRunOne = False Then ; Check if the flag is cleared <<<<<<<<<<<<<<<<<<<<<<<< ConsoleWrite(">Func 1 Ended Prematurely" & @CRLF) Return EndIf Sleep(100) Next ConsoleWrite(">Func 1 Ended" & @CRLF) Global $fRunOne = False EndFunc ;==>_Func_1_Run Func _Func_2() $fRunOne = False ; Clear the flag <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< For $i = 1 To 3 ConsoleWrite("+Func 2 Running" & @CRLF) Sleep(100) Next ConsoleWrite(">Func 2 Ended" & @CRLF) EndFunc ;==>_Func_2 Func _Exit() Exit EndFunc ;==>_Exit - Does the ConsoleWrite stops the function with a command to STDOUT? like the ConsoleWrite(">Func 1 Ended Prematurely" & @CRLF) ? - How do we initiate the stop?I just dont see where do we change the fRunOne to False I understand the general idea written on the wiki, and what I found in the forum, but can't seem to implement it, I'm pretty sure that I'm missing some small but crucial point Here is my code I try to interrupt for now (I'm certain that if I can manage it with the test, I can implement it with my other functions) #include <GUIConstantsEx.au3> Opt("GUIOnEventMode", 1) Global $fTestRun = False GUISetState(@SW_SHOW, $GUIform) ;GUI is created with ISN AutoIt Studio Func Test Do If $fTestRun = False Then ;ExitLoop? ConsoleWrite(">Test Ended Prematurely" & @CRLF) Return EndIf MsgBox(0, "Sooo programing", "Much Clever, seems so working") Sleep(2000) If $fTestRun = False Then ;ExitLoop? ConsoleWrite(">Test Ended Prematurely" & @CRLF) Return EndIf MsgBox(0, "Teszt", "ExitLoop solution?") Sleep(2000) If $fTestRun = False Then ;ExitLoop? ConsoleWrite(">Test Ended Prematurely" & @CRLF) Return EndIf MsgBox(0, "Teszt", "Nice try, but nope") $fTestRun = False Until 1 EndFunc ;==>Test Func TestRun() $fTestRun = True EndFunc ;==>TestRun While 1 Sleep(50) If $fTestRun Then Test() EndIf WEnd (Tried to edit it in to the Autoit bracket, it seems like it's in it from editor, but not in the post, sorry) I don't really understand the goal of the For... Next in Melba23's code, is it just a way to test it? (since the function runs a coupe of times this way) Thank you for your insight and happy holidays Edited December 17, 2014 by SorryButImaNewbie Link to comment Share on other sites More sharing options...
SorryButImaNewbie Posted December 17, 2014 Author Share Posted December 17, 2014 (edited) Okey, so I started to implement it because it worked, but that time still didn't realize how. With some testing and tweeking I just realize that the Return is the one actually "cancelling a function" as it basicly takes the code back to "nothing" and that takes it to the while...WEnd code thingy which runs while nothing happens in the GUI So the actuall interrupt can be just simply If $function = False Then Return EndIf It works, I see that but is it working because what I tried to describe above or for something else? Edited December 17, 2014 by SorryButImaNewbie Link to comment Share on other sites More sharing options...
MikahS Posted December 17, 2014 Share Posted December 17, 2014 (edited) You're initiating the stop function (func 2) by clicking the func 2 button, which makes the $fRunOne flag False, so when we leave func 2 we go back into func 1 to finish, but since the flag is set to false, it will initiate these lines: If $fRunOne = False Then ; Check if the flag is cleared <<<<<<<<<<<<<<<<<<<<<<<< ConsoleWrite(">Func 1 Ended Prematurely" & @CRLF) Return EndIf and interrupt func 1. Edited December 17, 2014 by MikahS Snips & Scripts My Snips: graphCPUTemp ~ getENVvarsMy Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4 Feel free to use any of my code for your own use. Forum FAQ Link to comment Share on other sites More sharing options...
computergroove Posted December 17, 2014 Share Posted December 17, 2014 I use this in most of my scripts to stop or exit based on a key press which is really useful for looping scripts that take over your mouse because if you cant stop the script then you might have to reboot to regain control over your computer. ;The Pause key on the keyboard will pause or unpause your script ;Esc will exit your script Global $Paused HotKeySet("{PAUSE}", "TogglePause") HotKeySet("{ESC}", "Terminate") $Paused = $Paused send("{pause}");Start the script paused While 1 Wend Func TogglePause() $Paused = NOT $Paused While $Paused sleep(100) ToolTip('Script is "Paused"',0,0) WEnd ToolTip("") EndFunc Func Terminate() Exit 0 EndFunc This will pause or exit your script but will not stop any external running processes. Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 17, 2014 Moderators Share Posted December 17, 2014 SorryButImaNewbie,How many more threads are you intending to start on this subject? You are correct that all you need to do in the function to be interrupted is look for a flag at intervals - the code I suggested in >your last thread looked for the flag during a pause (replacing the normal Sleep function). But the problem is getting the flag to change when you action a GUI control (rather then a Hotkey). As explained in the Interrupting a running function tutorial in the Wiki, in most cases AutoIt needs to exit the running function before it will run another (the exception is an OnEvent function run from the main loop as I showed in the code to which I have just linked) - which rather defeats the required behaviour of interrupting it! This means that you need to use a Windows message handler to intercept the control being actioned - basically peeking at it being actioned rather then leaving the event in the message queue to be actioned in its turn by AutoIt when the current function has ended. Hence the apparently complex code in the Wiki examples - it is not there to show how clever I am. Does that help clear the fog a bit. 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