<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.autoitscript.com/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Donnyh13</id>
	<title>AutoIt Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://www.autoitscript.com/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Donnyh13"/>
	<link rel="alternate" type="text/html" href="https://www.autoitscript.com/wiki/Special:Contributions/Donnyh13"/>
	<updated>2026-07-27T21:46:06Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Interrupting_a_running_function&amp;diff=15305</id>
		<title>Interrupting a running function</title>
		<link rel="alternate" type="text/html" href="https://www.autoitscript.com/w/index.php?title=Interrupting_a_running_function&amp;diff=15305"/>
		<updated>2026-07-24T14:39:56Z</updated>

		<summary type="html">&lt;p&gt;Donnyh13: Minor typo correction&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Tutorials]]&lt;br /&gt;
This is a common question and this tutorial explains how to do it within the limitations of Autoit.&lt;br /&gt;
&lt;br /&gt;
AutoIt queues function calls in both &#039;&#039;OnEvent&#039;&#039; and &#039;&#039;MessageLoop&#039;&#039; modes.  This means that it waits until a function is finished and the code is back in your idle &#039;&#039;While...WEnd&#039;&#039; loop before running the next.&lt;br /&gt;
&lt;br /&gt;
Here are 2 short scripts in both modes to show this in action.  Start &#039;&#039;Function 1&#039;&#039; by pressing its button and then immediately try to run &#039;&#039;Function 2&#039;&#039; by pressing its button a couple of times.  You will see from the console output that &#039;&#039;Function 2&#039;&#039; will only run &#039;&#039;&#039;AFTER&#039;&#039;&#039; &#039;&#039;Function 1&#039;&#039; has terminated - and then it will run as many times as you pressed the button.&lt;br /&gt;
&lt;br /&gt;
We start with &#039;&#039;MessageLoop&#039;&#039; mode:&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 #include &amp;lt;GUIConstantsEx.au3&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 $hGUI = GUICreate(&amp;quot;Test&amp;quot;, 500, 500)&lt;br /&gt;
 &lt;br /&gt;
 $hButton_1 = GUICtrlCreateButton(&amp;quot;Func One&amp;quot;, 10, 10, 80, 30)&lt;br /&gt;
 $hButton_2 = GUICtrlCreateButton(&amp;quot;Func Two&amp;quot;, 10, 50, 80, 30)&lt;br /&gt;
 &lt;br /&gt;
 GUISetState()&lt;br /&gt;
 &lt;br /&gt;
 While 1&lt;br /&gt;
     Switch GUIGetMsg()&lt;br /&gt;
         Case $GUI_EVENT_CLOSE&lt;br /&gt;
             Exit&lt;br /&gt;
         Case $hButton_1&lt;br /&gt;
             _Func_1()&lt;br /&gt;
         Case $hButton_2&lt;br /&gt;
         _Func_2()&lt;br /&gt;
     EndSwitch&lt;br /&gt;
 WEnd&lt;br /&gt;
 &lt;br /&gt;
 Func _Func_1()&lt;br /&gt;
     For $i = 1 To 20&lt;br /&gt;
         ConsoleWrite(&amp;quot;-Func 1 Running&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
         Sleep(100)&lt;br /&gt;
     Next&lt;br /&gt;
     ConsoleWrite(&amp;quot;&amp;gt;Func 1 Ended&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func _Func_2()&lt;br /&gt;
     For $i = 1 To 3&lt;br /&gt;
         ConsoleWrite(&amp;quot;+Func 2 Running&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
         Sleep(100)&lt;br /&gt;
     Next&lt;br /&gt;
     ConsoleWrite(&amp;quot;&amp;gt;Func 2 Ended&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-----------------------------------&lt;br /&gt;
And now in &#039;&#039;OnEvent&#039;&#039; mode:&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 #include &amp;lt;GUIConstantsEx.au3&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 Opt(&amp;quot;GUIOnEventMode&amp;quot;, 1)&lt;br /&gt;
 &lt;br /&gt;
 $hGUI = GUICreate(&amp;quot;Test&amp;quot;, 500, 500)&lt;br /&gt;
 GUISetOnEvent($GUI_EVENT_CLOSE, &amp;quot;_Exit&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
 $hButton_1 = GUICtrlCreateButton(&amp;quot;Func One&amp;quot;, 10, 10, 80, 30)&lt;br /&gt;
 GUICtrlSetOnEvent($hButton_1, &amp;quot;_Func_1&amp;quot;)&lt;br /&gt;
 $hButton_2 = GUICtrlCreateButton(&amp;quot;Func Two&amp;quot;, 10, 50, 80, 30)&lt;br /&gt;
 GUICtrlSetOnEvent($hButton_2, &amp;quot;_Func_2&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 GUISetState()&lt;br /&gt;
 &lt;br /&gt;
 While 1&lt;br /&gt;
     Sleep(10)&lt;br /&gt;
 WEnd&lt;br /&gt;
 &lt;br /&gt;
 Func _Func_1()&lt;br /&gt;
 For $i = 1 To 20&lt;br /&gt;
     ConsoleWrite(&amp;quot;-Func 1 Running&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
         Sleep(100)&lt;br /&gt;
     Next&lt;br /&gt;
     ConsoleWrite(&amp;quot;&amp;gt;Func 1 Ended&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func _Func_2()&lt;br /&gt;
     For $i = 1 To 3&lt;br /&gt;
         ConsoleWrite(&amp;quot;+Func 2 Running&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
         Sleep(100)&lt;br /&gt;
     Next&lt;br /&gt;
     ConsoleWrite(&amp;quot;&amp;gt;Func 2 Ended&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func _Exit()&lt;br /&gt;
     Exit&lt;br /&gt;
 EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-----------------------------------&lt;br /&gt;
So is it impossible to interrupt a running function?  Of course not or this tutorial would serve no purpose - but there are limitations on the type of function which can be interrupted.  &lt;br /&gt;
&lt;br /&gt;
If you are in &#039;&#039;OnEvent&#039;&#039; mode then there is an easy way to interrupt a running function, as long as the function is started within the main code and not by an &#039;&#039;OnEvent&#039;&#039; call.  This script shows how to do it by using a flag and checking if it is set - in this way the function is started by the main script and can be interrupted.&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 #include &amp;lt;GUIConstantsEx.au3&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 ; Declare a flag&lt;br /&gt;
 Global $fRunOne = False&lt;br /&gt;
 &lt;br /&gt;
 Opt(&amp;quot;GUIOnEventMode&amp;quot;, 1)&lt;br /&gt;
 &lt;br /&gt;
 $hGUI = GUICreate(&amp;quot;Test&amp;quot;, 500, 500)&lt;br /&gt;
 GUISetOnEvent($GUI_EVENT_CLOSE, &amp;quot;_Exit&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 $hButton_1 = GUICtrlCreateButton(&amp;quot;Func One&amp;quot;, 10, 10, 80, 30)&lt;br /&gt;
 GUICtrlSetOnEvent($hButton_1, &amp;quot;_Func_1&amp;quot;)&lt;br /&gt;
 $hButton_2 = GUICtrlCreateButton(&amp;quot;Func Two&amp;quot;, 10, 50, 80, 30)&lt;br /&gt;
 GUICtrlSetOnEvent($hButton_2, &amp;quot;_Func_2&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 GUISetState()&lt;br /&gt;
 &lt;br /&gt;
 While 1&lt;br /&gt;
     Sleep(10)&lt;br /&gt;
     ; Check if the flag has been set by the OnEvent function&lt;br /&gt;
     If $fRunOne Then&lt;br /&gt;
         ; Now start the &amp;quot;real&amp;quot; function from within the main code&lt;br /&gt;
         _Func_1_Run()&lt;br /&gt;
     EndIf&lt;br /&gt;
 WEnd&lt;br /&gt;
 &lt;br /&gt;
 Func _Func_1()&lt;br /&gt;
     ; Set the flag within the OnEvent function&lt;br /&gt;
     $fRunOne = True&lt;br /&gt;
 EndFunc   ;==&amp;gt;_Func_1&lt;br /&gt;
 &lt;br /&gt;
 Func _Func_1_Run()&lt;br /&gt;
     For $i = 1 To 20&lt;br /&gt;
         ConsoleWrite(&amp;quot;-Func 1 Running&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
         Sleep(100)&lt;br /&gt;
     Next&lt;br /&gt;
     ConsoleWrite(&amp;quot;&amp;gt;Func 1 Ended&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
     Global $fRunOne = False&lt;br /&gt;
 EndFunc   ;==&amp;gt;_Func_1_Run&lt;br /&gt;
 &lt;br /&gt;
 Func _Func_2()&lt;br /&gt;
     For $i = 1 To 3&lt;br /&gt;
         ConsoleWrite(&amp;quot;+Func 2 Running&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
         Sleep(100)&lt;br /&gt;
     Next&lt;br /&gt;
     ConsoleWrite(&amp;quot;&amp;gt;Func 2 Ended&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 EndFunc   ;==&amp;gt;_Func_2&lt;br /&gt;
 &lt;br /&gt;
 Func _Exit()&lt;br /&gt;
     Exit&lt;br /&gt;
 EndFunc   ;==&amp;gt;_Exit&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-----------------------------------&lt;br /&gt;
However, this method does not work in &#039;&#039;MessageLoop&#039;&#039; mode, nor when you want to interrupt a function which was started by an &#039;&#039;OnEvent&#039;&#039; call in &#039;&#039;OnEvent&#039;&#039; mode.  In these cases the function you want to interrupt must have regular checks of a flag which we set when we want to interrupt.  If the function does not have a suitable loop where such a check is easy to make, you might not be able to interrupt it.&lt;br /&gt;
&lt;br /&gt;
Let us now look at the 2 ways an AutoIt script can break into a suitable function: a &#039;&#039;&#039;HotKey&#039;&#039;&#039; and a &#039;&#039;&#039;Windows message handler&#039;&#039;&#039;.  The first is easier to code, but cannot be linked to a control on your GUI; the latter is a little more complicated to code, but can be linked to a control.&lt;br /&gt;
&lt;br /&gt;
How do they work?&lt;br /&gt;
&lt;br /&gt;
Let us start with the &#039;&#039;HotKey&#039;&#039;.  From the AutoIt Help file: &#039;&#039;A hotkey-press *typically* interrupts the active AutoIt function/statement and runs its user function until it completes or is interrupted&#039;&#039;.  So a &#039;&#039;HotKey&#039;&#039; press is designed to interrupt your running function - just what we need.  But the running function will resume after the &#039;&#039;HotKey&#039;&#039; function terminates - so, as explained above, we use the &#039;&#039;HotKey&#039;&#039; function to set a flag which is looked for by our running function.  Remember that this flag must be declared as a &#039;&#039;Global&#039;&#039; variable because it needs to be seen by both functions.&lt;br /&gt;
&lt;br /&gt;
Now many coders do not like using &#039;&#039;HotKey&#039;&#039;s because they remain active while a script is running and can interfere with other apps.  One way to get around this is to use &#039;&#039;Accelerator&#039;&#039; keys - these look very much like &#039;&#039;HotKey&#039;&#039;s to the user, but are only active in the script in which they are declared.  Although &#039;&#039;Accelerator&#039;&#039; keys look like &#039;&#039;HotKey&#039;&#039;s, they are tied to GUI controls and you must use the &#039;&#039;message handler&#039;&#039; method explained below if you want to use them to interrupt a running function.&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;Windows message handler&#039;&#039; method is a little more complicated and if you are not used to working with them, you might want to read the [[Tutorial GUIRegisterMsg|GUIRegisterMsg]] tutorial first.&lt;br /&gt;
&lt;br /&gt;
What we do here is intercept the message that your GUI control sends when it is activated - the same message that AutoIt queues to run the required code once the running function terminates.  We use our own message to get a sneak preview of this queuing and use it to set a similar flag to that described above.  If our running function sees the flag, it terminates and AutoIt then runs the function waiting in the queue - simple really!&lt;br /&gt;
&lt;br /&gt;
Here are scripts using both methods to show how &#039;&#039;HotKey&#039;&#039;s, &#039;&#039;Accelerator&#039;&#039; keys and GUI controls can interrupt a running function.  Just as before, start &#039;&#039;Function_1&#039;&#039; by pressing its button  - but now you can interrupt it at will!&lt;br /&gt;
&lt;br /&gt;
First in &#039;&#039;MessageLoop&#039;&#039; mode:&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 #include &amp;lt;GUIConstantsEx.au3&amp;gt;&lt;br /&gt;
 #include &amp;lt;WindowsConstants.au3&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 ; Set a HotKey&lt;br /&gt;
 HotKeySet(&amp;quot;x&amp;quot;, &amp;quot;_Interrupt&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 ; Declare a flag&lt;br /&gt;
 $fInterrupt = 0&lt;br /&gt;
 &lt;br /&gt;
 $hGUI = GUICreate(&amp;quot;Test&amp;quot;, 500, 500)&lt;br /&gt;
 &lt;br /&gt;
 $hButton_1 = GUICtrlCreateButton(&amp;quot;Func One&amp;quot;, 10, 10, 80, 30)&lt;br /&gt;
 $hButton_2 = GUICtrlCreateButton(&amp;quot;Func Two&amp;quot;, 10, 50, 80, 30)&lt;br /&gt;
 &lt;br /&gt;
 ; Create a dummy control for the Accelerator to action when pressed&lt;br /&gt;
 $hAccelInterupt = GUICtrlCreateDummy()&lt;br /&gt;
 ; Set an Accelerator key to action the dummy control&lt;br /&gt;
 Dim $AccelKeys[1][2]=[ [&amp;quot;z&amp;quot;, $hAccelInterupt] ]&lt;br /&gt;
 GUISetAccelerators($AccelKeys)&lt;br /&gt;
 &lt;br /&gt;
 GUISetState()&lt;br /&gt;
 &lt;br /&gt;
 ; Intercept Windows command messages with our own handler&lt;br /&gt;
 GUIRegisterMsg($WM_COMMAND, &amp;quot;_WM_COMMAND&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 While 1&lt;br /&gt;
     Switch GUIGetMsg()&lt;br /&gt;
         Case $GUI_EVENT_CLOSE&lt;br /&gt;
             Exit&lt;br /&gt;
         Case $hButton_1&lt;br /&gt;
             _Func_1()&lt;br /&gt;
         Case $hButton_2&lt;br /&gt;
             _Func_2()&lt;br /&gt;
     EndSwitch&lt;br /&gt;
 WEnd&lt;br /&gt;
 &lt;br /&gt;
 Func _Func_1()&lt;br /&gt;
     ; Make sure the flag is cleared&lt;br /&gt;
     $fInterrupt = 0&lt;br /&gt;
     For $i = 1 To 20&lt;br /&gt;
         ConsoleWrite(&amp;quot;-Func 1 Running&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
         ; Look for the flag&lt;br /&gt;
         If $fInterrupt &amp;lt;&amp;gt; 0 Then&lt;br /&gt;
             ; The flag was set&lt;br /&gt;
             Switch $fInterrupt&lt;br /&gt;
                 Case 1&lt;br /&gt;
                     ConsoleWrite(&amp;quot;!Func 1 interrrupted by Func 2&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
                 Case 2&lt;br /&gt;
                     ConsoleWrite(&amp;quot;!Func 1 interrrupted by HotKey&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
                 Case 3&lt;br /&gt;
                     ConsoleWrite(&amp;quot;!Func 1 interrrupted by Accelerator&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
             EndSwitch&lt;br /&gt;
             Return&lt;br /&gt;
         EndIf&lt;br /&gt;
         Sleep(100)&lt;br /&gt;
     Next&lt;br /&gt;
     ConsoleWrite(&amp;quot;&amp;gt;Func 1 Ended&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func _Func_2()&lt;br /&gt;
     For $i = 1 To 3&lt;br /&gt;
         ConsoleWrite(&amp;quot;+Func 2 Running&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
         Sleep(100)&lt;br /&gt;
     Next&lt;br /&gt;
     ConsoleWrite(&amp;quot;&amp;gt;Func 2 Ended&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func _Interrupt()&lt;br /&gt;
     ; The HotKey was pressed so set the flag&lt;br /&gt;
     $fInterrupt = 2&lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)&lt;br /&gt;
     ; The Func 2 button was pressed so set the flag&lt;br /&gt;
     If BitAND($wParam, 0x0000FFFF) =  $hButton_2 Then $fInterrupt = 1&lt;br /&gt;
     ; The dummy control was actioned by the Accelerator key so set the flag&lt;br /&gt;
     If BitAND($wParam, 0x0000FFFF) =  $hAccelInterupt Then $fInterrupt = 3&lt;br /&gt;
     Return $GUI_RUNDEFMSG&lt;br /&gt;
 EndFunc   ;==&amp;gt;_WM_COMMAND&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-----------------------------------&lt;br /&gt;
And now in &#039;&#039;OnEvent&#039;&#039; mode:&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 #include &amp;lt;GUIConstantsEx.au3&amp;gt;&lt;br /&gt;
 #include &amp;lt;WindowsConstants.au3&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 ; Set a HotKey&lt;br /&gt;
 HotKeySet(&amp;quot;x&amp;quot;, &amp;quot;_Interrupt_HotKey&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 ; Declare a flag&lt;br /&gt;
 $fInterrupt = 0&lt;br /&gt;
 &lt;br /&gt;
 Opt(&amp;quot;GUIOnEventMode&amp;quot;, 1)&lt;br /&gt;
 &lt;br /&gt;
 $hGUI = GUICreate(&amp;quot;Test&amp;quot;, 500, 500)&lt;br /&gt;
 GUISetOnEvent($GUI_EVENT_CLOSE, &amp;quot;_Exit&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 $hButton_1 = GUICtrlCreateButton(&amp;quot;Func One&amp;quot;, 10, 10, 80, 30)&lt;br /&gt;
 GUICtrlSetOnEvent($hButton_1, &amp;quot;_Func_1&amp;quot;)&lt;br /&gt;
 $hButton_2 = GUICtrlCreateButton(&amp;quot;Func Two&amp;quot;, 10, 50, 80, 30)&lt;br /&gt;
 GUICtrlSetOnEvent($hButton_2, &amp;quot;_Func_2&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 ; Create a dummy control for the Accelerator to action&lt;br /&gt;
 $hAccelInterupt = GUICtrlCreateDummy()&lt;br /&gt;
 GUICtrlSetOnEvent($hAccelInterupt, &amp;quot;_Interrupt_Accel&amp;quot;)&lt;br /&gt;
 ; Set an Accelerator key to action the dummy control&lt;br /&gt;
 Dim $AccelKeys[1][2]=[ [&amp;quot;z&amp;quot;, $hAccelInterupt] ]&lt;br /&gt;
 GUISetAccelerators($AccelKeys)&lt;br /&gt;
 &lt;br /&gt;
 GUISetState()&lt;br /&gt;
 &lt;br /&gt;
 ; Intercept Windows command messages with our own handler&lt;br /&gt;
 GUIRegisterMsg($WM_COMMAND, &amp;quot;_WM_COMMAND&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 While 1&lt;br /&gt;
 	Sleep(10)&lt;br /&gt;
 WEnd&lt;br /&gt;
 &lt;br /&gt;
 Func _Func_1()&lt;br /&gt;
     ; Make sure the flag is cleared&lt;br /&gt;
     $fInterrupt = 0&lt;br /&gt;
     For $i = 1 To 20&lt;br /&gt;
         ConsoleWrite(&amp;quot;-Func 1 Running&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
         ; Look for the flag&lt;br /&gt;
         If $fInterrupt &amp;lt;&amp;gt; 0 Then&lt;br /&gt;
             ; The flag was set&lt;br /&gt;
             Switch $fInterrupt&lt;br /&gt;
                 Case 1&lt;br /&gt;
                     ConsoleWrite(&amp;quot;!Func 1 interrrupted by Func 2&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
                 Case 2&lt;br /&gt;
                     ConsoleWrite(&amp;quot;!Func 1 interrrupted by HotKey&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
                 Case 3&lt;br /&gt;
                     ConsoleWrite(&amp;quot;!Func 1 interrrupted by Accelerator&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
             EndSwitch&lt;br /&gt;
             Return&lt;br /&gt;
         EndIf&lt;br /&gt;
         Sleep(100)&lt;br /&gt;
     Next&lt;br /&gt;
     ConsoleWrite(&amp;quot;&amp;gt;Func 1 Ended&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func _Func_2()&lt;br /&gt;
     For $i = 1 To 3&lt;br /&gt;
         ConsoleWrite(&amp;quot;+Func 2 Running&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
         Sleep(100)&lt;br /&gt;
     Next&lt;br /&gt;
     ConsoleWrite(&amp;quot;&amp;gt;Func 2 Ended&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func _Exit()&lt;br /&gt;
     Exit&lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func _Interrupt_HotKey()&lt;br /&gt;
     ; The HotKey was pressed so set the flag&lt;br /&gt;
     $fInterrupt = 2&lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func _Interrupt_Accel()&lt;br /&gt;
     ; This is an empty function for the dummy control linked to the Accelerator key&lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)&lt;br /&gt;
     ; The Func 2 button was pressed so set the flag&lt;br /&gt;
     If BitAND($wParam, 0x0000FFFF) =  $hButton_2 Then $fInterrupt = 1&lt;br /&gt;
     ; The dummy control was actioned by the Accelerator key so set the flag&lt;br /&gt;
     If BitAND($wParam, 0x0000FFFF) =  $hAccelInterupt Then $fInterrupt = 3&lt;br /&gt;
     Return $GUI_RUNDEFMSG&lt;br /&gt;
 EndFunc   ;==&amp;gt;_WM_COMMAND&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-----------------------------------&lt;br /&gt;
But what if you want to have a longish &#039;&#039;Sleep&#039;&#039; or some other function which waits for a fair time inside the running function?  If you add a &#039;&#039;Sleep(5000)&#039;&#039; to &#039;&#039;Func_1&#039;&#039; in the above examples, you will see that you have to wait until the function returns from the &#039;&#039;Sleep&#039;&#039; before the interrupt is actioned, which is not what we want:&lt;br /&gt;
----------------------------------&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 Func _Func_1()&lt;br /&gt;
 	; Make sure the flag is cleared&lt;br /&gt;
 	$fInterrupt = 0&lt;br /&gt;
 	For $i = 1 To 20&lt;br /&gt;
 		ConsoleWrite(&amp;quot;-Func 1 Running&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 		; Add a long Sleep ; &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;br /&gt;
 		Sleep(5000)&lt;br /&gt;
 		; Look for the flag&lt;br /&gt;
          	If $fInterrupt &amp;lt;&amp;gt; 0 Then&lt;br /&gt;
 			; The flag was set&lt;br /&gt;
 			Switch $fInterrupt&lt;br /&gt;
 				Case 1&lt;br /&gt;
 					ConsoleWrite(&amp;quot;!Func 1 interrrupted by Func 2&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 				Case 2&lt;br /&gt;
 					ConsoleWrite(&amp;quot;!Func 1 interrrupted by HotKey&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 				Case 3&lt;br /&gt;
 					ConsoleWrite(&amp;quot;!Func 1 interrrupted by Accelerator&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 			EndSwitch&lt;br /&gt;
 			Return&lt;br /&gt;
 		EndIf&lt;br /&gt;
 		Sleep(100)&lt;br /&gt;
 	Next&lt;br /&gt;
 	ConsoleWrite(&amp;quot;&amp;gt;Func 1 Ended&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 EndFunc   ;==&amp;gt;_Func_1&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&lt;br /&gt;
The answer is to put the blocking function inside a wrapper function which does check for the interrupt flag at regular intervals.  Replace the Sleep line with a call to the wrapper function and add the wrapper function to the script:&lt;br /&gt;
&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 ; Use a wrapper function in place of the blocking function&lt;br /&gt;
 Func _Func_1()&lt;br /&gt;
 	; Make sure the flag is cleared&lt;br /&gt;
 	$fInterrupt = 0&lt;br /&gt;
 	For $i = 1 To 20&lt;br /&gt;
 		ConsoleWrite(&amp;quot;-Func 1 Running&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 		; Run a modified Sleep function which checks for the interrupt&lt;br /&gt;
 		If _Interrupt_Sleep(5000) Then ; &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;br /&gt;
 			; The flag was set&lt;br /&gt;
 			Switch $fInterrupt&lt;br /&gt;
 				Case 1&lt;br /&gt;
 					ConsoleWrite(&amp;quot;!Func 1 interrrupted by Func 2&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 				Case 2&lt;br /&gt;
 					ConsoleWrite(&amp;quot;!Func 1 interrrupted by HotKey&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 				Case 3&lt;br /&gt;
 					ConsoleWrite(&amp;quot;!Func 1 interrrupted by Accelerator&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 			EndSwitch&lt;br /&gt;
 			Return&lt;br /&gt;
 		EndIf&lt;br /&gt;
 		Sleep(100)&lt;br /&gt;
 	Next&lt;br /&gt;
 	ConsoleWrite(&amp;quot;&amp;gt;Func 1 Ended&amp;quot; &amp;amp; @CRLF)&lt;br /&gt;
 EndFunc   ;==&amp;gt;_Func_1&lt;br /&gt;
 &lt;br /&gt;
 ; And here is the wrapper function itself&lt;br /&gt;
 Func _Interrupt_Sleep($iDelay)&lt;br /&gt;
 	; Get a timestamp&lt;br /&gt;
 	Local $iBegin = TimerInit()&lt;br /&gt;
 	; And run a tight loop&lt;br /&gt;
 	Do&lt;br /&gt;
 		; Use a minimum Sleep (could also be a WinWaitActive with a short timeout)&lt;br /&gt;
 		Sleep(10)&lt;br /&gt;
 		; Look for the interrrupt&lt;br /&gt;
 		If $fInterrupt Then&lt;br /&gt;
 			; And return True immediately if set&lt;br /&gt;
 			Return True&lt;br /&gt;
 		EndIf&lt;br /&gt;
 	Until TimerDiff($iBegin) &amp;gt; $iDelay&lt;br /&gt;
 	; Return False if timed out and no interrupt was set&lt;br /&gt;
 	Return False&lt;br /&gt;
 EndFunc   ;==&amp;gt;_Interrupt_Sleep&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-----------------------------------&lt;br /&gt;
So there are the ways available in AutoIt to interrupt a running function.  Remember that you must be in &#039;&#039;OnEvent&#039;&#039; mode and start the function from the main code - or have a function which checks internally whether it ought to allow an interruption.  You may need to use a wrapper function if you want to use a blocking function such as &#039;&#039;Sleep&#039;&#039; or &#039;&#039;WinWaitActive&#039;&#039;.  In all other cases you are are not going to be able to interrupt your function.&lt;/div&gt;</summary>
		<author><name>Donnyh13</name></author>
	</entry>
</feed>