User1234321
User1234321

Reputation: 341

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

Upvotes: 2

Views: 3886

Answers (4)

Mojtaba Rezaeian
Mojtaba Rezaeian

Reputation: 8736

I wrote this code for such job in my own scripts (activated by pressing 🪟 winkey+W to suspend/unsuspend the script):

    #w:: ; winkey + w
        Suspend, Permit ; mark the current subroutine as being exempt from suspension.
        Suspend
        return ; end of the subroutine

Upvotes: 1

EvgEnZh
EvgEnZh

Reputation: 879

I found a trick to relaunch current script without a separate launcher script:

Esc::
Reload
ExitApp

Reload, obviously, reloads the current script. (NOTE: Command-line parameters aren't passed down to the new instance.) And then ExitApp makes sure current instance dies right away and even if the reload fails.

The effect is similar to the solution by Hampus, but doesn't require hardcoding or otherwise specifying any file names within the scripts, and is much more convenient for scripts with multiple long-running hotkeys, which would require a separate script file each with the launcher approach.

Upvotes: 0

RndCoder
RndCoder

Reputation: 333

to do that:
1.go to the system tray (wintray)
2.right-click on the autohotkey logo symbol.
3.click on suspend hotkeys

What happens when you click on pause script? Something horrible.
ex:if you remapped the a key key, that useful key will be shutdown.

hope this helped.

Upvotes: 1

Hampus Larsson
Hampus Larsson

Reputation: 3100

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.

Upvotes: 1

Related Questions