Make script kill itself on exit

Post your working scripts, libraries and tools
Bluscream
Posts: 15
Joined: 10 Jan 2017, 23:12
GitHub: Bluscream

Make script kill itself on exit

16 Apr 2020, 02:30

Some times i get the annoying "Could not close the previous instance of this script. Keep waiting?" error so i decided to make a snippet that kills the script instead:

Code: Select all

OnExit, ExitSub
return

ExitSub:
    ahk_exe := StrSplit(A_AhkPath, "\")
    ahk_exe := ahk_exe[ahk_exe.MaxIndex()]
    query := "Select * from Win32_Process where Name = '" . ahk_exe . "'"
    ; MsgBox % query
    for objItem in ComObjGet("winmgmts:").ExecQuery(query)
    {
        path := """" . A_ScriptFullPath . """"
        is_path := InStr(objItem.CommandLine, path)
        if (is_path) {
            ; WinKill, % "ahk_pid " . objItem.ProcessId
            Process, Close, % objItem.ProcessId
        }
        ; MsgBox % "path: " . path . "`r`ncmdline: " . objItem.CommandLine . "`r`nis_path: " . is_path
    }
Tidy version:

Code: Select all

OnExit, ExitSub
return

ExitSub:
    ahk_exe := StrSplit(A_AhkPath, "\")
    for objItem in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where Name = '" . ahk_exe[ahk_exe.MaxIndex()] . "'")
    {
        if (InStr(objItem.CommandLine, """" . A_ScriptFullPath . """")) {
            Process, Close, % objItem.ProcessId
        }
    }
User avatar
Xtra
Posts: 2038
Joined: 02 Oct 2015, 12:15

Re: Make script kill itself on exit

16 Apr 2020, 03:21

Try: SubStr(A_AhkPath, InStr(A_AhkPath, "\", , 0) + 1)

It will save one line and not use an array.
HTH

Return to “Scripts and Functions”

Who is online

Users browsing this forum: flacito and 18 guests