Page 1 of 1

WinSetTitle breaks #SingleInstance Force

Posted: 26 Sep 2017, 01:07
by pneumatic
Renaming the script's own window, even to the same name it's normally given by AHK, causes #SingleInstance Force to no longer work

Code: Select all

#SingleInstance Force
#Persistent
#NoEnv
SetBatchLines -1
DetectHiddenWindows On
WinSetTitle ,% "ahk_id " A_ScriptHwnd ,,MyScript.ahk - AutoHotkey v1.1.24.03
Return

Re: WinSetTitle breaks #SingleInstance Force

Posted: 26 Sep 2017, 06:31
by jeeswg
If you change it to exactly the same title, #SingleInstance force still appears to work, but yes, in the general case, this is very surprising.

Code: Select all

#SingleInstance force
#Persistent
#NoEnv
SetBatchLines, -1
DetectHiddenWindows, On
WinGetTitle, vWinTitle, % "ahk_id " A_ScriptHwnd
WinSetTitle , % "ahk_id " A_ScriptHwnd,, % vWinTitle "x" ;modify title
Sleep 1000
WinSetTitle , % "ahk_id " A_ScriptHwnd,, % vWinTitle ;restore title
return

Re: WinSetTitle breaks #SingleInstance Force

Posted: 26 Sep 2017, 08:10
by just me
To determine if a script is already running AHK is searching for windows of class AutoHotkey (hidden main window) with exactly the same title the new main window would get on creation. This is actually a rather simple method but has been sufficient for many years. Apparently, nobody using #SingleInstance, Force had a reason to change the main window's title as yet.

We could add something like
You must not change the title of the script's hidden main window if you want to prevent new instances of the script from being launched.
to the docs.