Restart a script every time it is activated

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jsfuojgbnf
Posts: 1
Joined: 18 Apr 2021, 18:51

Restart a script every time it is activated

18 Apr 2021, 19:06

Code: Select all

xButton2::
Send, 4
Sleep, 100
Send, 2
Sleep, 50
Click, Left, 1
Sleep, 100
Send, 8
Sleep, 2000
Send, 4
Return
I want to make the program restart every time I hit xbutton2. Currently, if I hit xbutton2 while the script is running it will do nothing. I want to end the previous thing running and start it again if I hit xbutton2 in the middle of the script.
Lowy
Posts: 2
Joined: 25 Aug 2020, 12:14

Re: Restart a script every time it is activated

18 Apr 2021, 22:11

Adding Reload to the last last line of the hotkey will reload the script.

See: https://www.autohotkey.com/docs/commands/Reload.htm
Pepineros
Posts: 45
Joined: 16 Apr 2018, 17:26
Location: Ireland

Re: Restart a script every time it is activated

19 Apr 2021, 11:58

By default, a given hotkey or hotstring subroutine cannot be run a second time if it is already running. Use #MaxThreadsPerHotkey to change this behavior.
from https://www.autohotkey.com/docs/misc/Threads.htm

You can set #MaxThreadsPerHotkey, 2 at the top of your script. See if you can make it work and come back if not - I'm on my phone and can't run a quick script to tell you exactly what to write.
User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Restart a script every time it is activated

19 Apr 2021, 14:21

Lowy wrote: Adding Reload to the last last line of the hotkey will reload the script. It uses a .ini file so it knows upon reloading that it's should immediately execute without waiting for the hotkey to be pressed again.

See: https://www.autohotkey.com/docs/commands/Reload.htm
That won't get it to start over immediately if he presses the hotkey in the middle of the subroutine executing.


This will cause it to reload and start over again if the hotkey is pressed again while it's running:

Code: Select all

IniRead, Restarted, restart.ini, Main, Restart, 0
if Restarted {
	IniWrite, 0, restart.ini, Main, Restart
	gosub, Main
}
return

#If Running
XButton2::
	IniWrite, 1, restart.ini, Main, Restart
Reload

#If !Running
Main:
XButton2::
	Running := 1
	Send, 4
	Sleep, 100
	Send, 2
	Sleep, 50
	Click, Left, 1
	Sleep, 100
	Send, 8
	Sleep, 2000
	Send, 4
	Running := 0
Return

Esc::ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 270 guests