You could use a lockfile (or in this example a lockregistry):
Code:
#SingleInstance Off
#Persistent
has_params = %0%
if (!has_params)
{
RegRead, is_running, HKCU, Software\SingleInstanceScript, is_running
if (is_running)
{
MsgBox, 48, Already Running, Another instance of this script is already running. This script will now exit.
ExitApp
}
RegWrite, REG_DWORD, HKCU, Software\SingleInstanceScript, is_running, 1
OnExit, delete_key
}
return
delete_key:
RegDelete, HKCU, Software\SingleInstanceScript
ExitApp
[Edit]If something happens and the script doesn't run the OnExit (computer locks up, power goes out, etc.), then you wouldn't be able to start the program without parameters without deleting the registry key, so I added a check (from Lexikos' script) so it will always delete the key if there is only 1 process running.
Code:
#SingleInstance Off
#Persistent
WinGet instances, List, %A_ScriptFullPath% ahk_class AutoHotkey
if (instances = 1)
RegDelete, HKCU, Software\SingleInstanceScript
has_params = %0%
if (!has_params)
{
RegRead, is_running, HKCU, Software\SingleInstanceScript, is_running
if (is_running)
{
MsgBox, 48, Already Running, Another instance of this script is already running. This script will now exit.
ExitApp
}
RegWrite, REG_DWORD, HKCU, Software\SingleInstanceScript, is_running, 1
OnExit, delete_key
}
return
delete_key:
RegDelete, HKCU, Software\SingleInstanceScript
ExitApp