avoid 'Could not close the previous instance of this script' warnings

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 6655
Joined: 19 Dec 2016, 01:58
Location: UK

avoid 'Could not close the previous instance of this script' warnings

03 Dec 2018, 16:58

Sometimes when the PC is under heavy load, all of the hotkeys in all of my scripts are forgotten. And if I reload those scripts (by clicking Reload or sending a WM_COMMAND message), I repeatedly see the Could not close the previous instance of this script. Keep waiting? warning MsgBox.

I realised that if I close a script instead of reloading it, I don't see the MsgBox. So, a better way to 'reload' a script is to instruct a script to close, wait for the process to no longer exist, and reopen the script. Advantages: (a) no warning MsgBoxes to dismiss (or accidentally click 'No' on), (b) it 'reloads' faster.

So, my solution is to create a script, passing it a PID, the script closes the script with the particular PID, and reopens it.

Code: Select all

;reload script by jeeswg
;note: A_Args requires AHK v1.1.27+

;==================================================

; ;e.g. save this script as 'reload script.ahk'
; ;and use these 5 lines of code to run it
; vPathScript := A_ScriptDir "\AutoHotkey.ahk"
; vPathReload := A_ScriptDir "\reload script.ahk"
; DetectHiddenWindows, On
; WinGet, vPID, PID, % vPathScript " - AutoHotkey v ahk_class AutoHotkey"
; Run, % Chr(34) vPathReload Chr(34) " " vPID

;==================================================

#SingleInstance off
#KeyHistory 0
#NoTrayIcon
ListLines, Off

;note:
;'Could not close the previous instance of this script.  Keep waiting?'
;Reload: shows 'close' warning
;Close: doesn't show 'close' warning

DetectHiddenWindows, On
if !(vPID := A_Args.1)
|| !(hWnd := WinExist("ahk_class AutoHotkey ahk_pid" vPID))
	return

oWMI := ComObjGet("winmgmts:")
oQueryEnum := oWMI.ExecQuery("Select * from Win32_Process where ProcessId=" vPID)._NewEnum()
oProcess := ""
if oQueryEnum[oProcess]
	vCmdLn := oProcess.CommandLine
oWMI := oQueryEnum := oProcess := ""
if (vCmdLn = "")
	return

WinGetTitle, vWinTitle, % "ahk_id " hWnd
PostMessage, 0x111, 65405,,, % "ahk_id " hWnd ;ID_FILE_EXIT := 65405

;note: it is not necessary to add '/restart' to the command line of a script,
;however, AutoHotkey normally adds this when Reload is used,
;(and some scripts I have show a SplashText only if '/restart' is present in the command line,
;to confirm that the script was successfully reloaded)
if !InStr(vCmdLn, " /restart ")
{
	vCmdLn := Trim(vCmdLn)
	;vCmdLn0 := vCmdLn
	if (SubStr(vCmdLn, 1, 1) = Chr(34))
	{
		if !(SubStr(vCmdLn, 1, 2) = Chr(34) " ")
			vCmdLn := StrReplace(vCmdLn, Chr(34) " ", Chr(34) " /restart ",, 1)
	}
	else
		vCmdLn := StrReplace(vCmdLn, " ", " /restart ",, 1)
	;MsgBox, % vCmdLn0 "`r`n`r`n" vCmdLn
}

Process, WaitClose, % vPID
Run, % vCmdLn
;MsgBox, % "reloaded:`r`n" vWinTitle
return
Links:
list of AutoHotkey WM_COMMAND IDs (e.g. Reload/Edit/Suspend/ListVars on another script) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=27824
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
malcev
Posts: 197
Joined: 12 Aug 2014, 12:37

Re: avoid 'Could not close the previous instance of this script' warnings

04 Dec 2018, 03:40

Can You do it like this?

Code: Select all

#SingleInstance Off  
DetectHiddenWindows, On
CurPID := DllCall("GetCurrentProcessId")
WinGet, List, List, %A_ScriptFullPath% ahk_class AutoHotkey
Loop % List
{ 
    WinGet, PID, PID, % "ahk_id" List%A_Index%
    If (PID != CurPID)
        Process, Close, %PID% 
}
; Any code
User avatar
jeeswg
Posts: 6655
Joined: 19 Dec 2016, 01:58
Location: UK

Re: avoid 'Could not close the previous instance of this script' warnings

04 Dec 2018, 06:30

- @malcev: Yes and no. That forcibly closes the scripts, versus AHK closing them neatly. And it doesn't reopen the scripts.
- Although the error message is about *closing* scripts, you only the get that 'close' error when you try to *reload* a script. Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
gwarble
Posts: 376
Joined: 30 Sep 2013, 15:01

Re: avoid 'Could not close the previous instance of this script' warnings

04 Dec 2018, 18:20

I usually do it like this from within the script:

Code: Select all

pDHW := A_DetectHiddenWindows
 DetectHiddenWindows On
 WinGet, List, List, ahk_exe %Exe%
 Loop %List% 
  SendMessage,0x111,65405,0,, % "ahk_id " List%A_Index% 
 DetectHiddenWindows %pDHW%
or use malcev's version above, but use SendMessage,0x111,65405,0... to close it more gracefully than Process, close...
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
Helgef
Posts: 3898
Joined: 17 Jul 2016, 01:02
Contact:

Re: avoid 'Could not close the previous instance of this script' warnings

05 Dec 2018, 04:14

(b) it 'reloads' faster.
How come? If your script or computer is unresponsive, you will still have to wait for it respond and for the script to close.

When you reload a (single instance) script, it launches a new instance of itself, the new instance will tell the old one to close when the the new instance has loaded successfully. The new instance will then wait to run until the old one has closed.

Cheers.
User avatar
jeeswg
Posts: 6655
Joined: 19 Dec 2016, 01:58
Location: UK

Re: avoid 'Could not close the previous instance of this script' warnings

05 Dec 2018, 09:58

Reloading by sending the 'reload' message.
v.
Reloading by sending the 'close' message, waiting, and reopening.
The latter is faster because you don't have to dismiss any 'Could not close ...' MsgBoxes.
The former is often very slow and the MsgBoxes interfere with doing other things.
The latter is usually fairly swift, and doesn't take too long.

v.
Forcibly closing the process.
This is fastest, but you're forcibly closing the process.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 3898
Joined: 17 Jul 2016, 01:02
Contact:

Re: avoid 'Could not close the previous instance of this script' warnings

05 Dec 2018, 10:12

The latter is faster because you don't have to dismiss any 'Could not close ...' MsgBoxes.
Assuming the script closes while the msgbox is showing that is.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: WuffTheCoder and 37 guests