How to stop a running AHK script whitout closing the script?

I have written an AHK script that sends a set of keyboard keys when the hotkey ^!e:: is pressed. This script needs a couple of seconds to complete, but sometimes I need to stop the execution before the script has finished doing its task. I know that this can be done by setting the ExitApp function to another hotkey, but this will close the script. So, if I need to trigger the script again, it will be necessary to open the .ahk file anew.

How could the script be stopped without closing it? I would also need that, when it is triggered again, it sends the keys from the beginning, and not from where it left off when I stopped it.

Edit: let's take as an example this code,

^!q::
SoundBeep, 208
SoundBeep, 155
SoundBeep, 208
SoundBeep, 196
SoundBeep, 208
return

Solution 1:

What you can do to resolve the issue is to have two different script-files.

MainScript.ahk:

; Starts the secondary-file into its own process:
^!l::run SecondaryScript.ahk  ; Ctrl+Alt+L

SecondaryScript.ahk:

#SingleInstance Force

SoundBeep, 208
SoundBeep, 155
SoundBeep, 208
SoundBeep, 196
SoundBeep, 208
ExitApp

^!q::ExitApp   ; Ctrl+Alt+Q

After the SecondaryScript has finished its auto-executing of code, then it will issue ExitApp which will kill the process that the file is currently being executed inside of.

If you want to abort the execution, then you can use the hotkey that is defined under the Auto-Execute portion of the script.