[Solved] ExitApp fails to close Script when paired with Process Exist Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
t4akawolf
Posts: 12
Joined: 30 Jul 2016, 03:30

[Solved] ExitApp fails to close Script when paired with Process Exist

30 Jul 2016, 03:53

This is the AHK script. Like I want it to, it executes script.bat hidden when I run it, showing only an icon in the tray area. It also has the option to hide/show script.bat's console window in the context menu, and the option to exit script.bat and then itself.
I've noticed, though, that if I exit the console window directly, the AHK script just sits there in the tray, and clicking on Close in the right click menu doesn't do anything. I have to run the script again (causing it to run script.bat again), then click on Close in the context menu. The last part of the script is supposed to prevent this from happening, by checking every second if cmd is running, then exiting itself when it finds that it isn't. What am I doing wrong?

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#NoTrayIcon
#Persistent
#SingleInstance force

global script

/* Setup Tray icon and add item that will handle
* double click events
*/
Menu Tray, NoStandard
Menu Tray, Icon
Menu Tray, Icon, icon.ico
Menu Tray, Add, Show / Hide, TrayClick
Menu Tray, Add, Close, CloseItem
Menu Tray, Default, Show / Hide

;// Run program or batch file hidden
DetectHiddenWindows On
Run script.bat,, Hide, PID
WinWait ahk_pid %PID%
script := WinExist()
DetectHiddenWindows Off
return

TrayClick:
OnTrayClick()
return

;// Show / hide program or batch file on double click
OnTrayClick() {
    if DllCall("IsWindowVisible", "Ptr", script) {
        WinHide ahk_id %script%

    } else {
        WinShow ahk_id %script%
        WinActivate ahk_id %script%
    }
}

CloseItem() {

       DetectHiddenWindows On
       WinWait ahk_class ConsoleWindowClass
       Process, Close, cmd.exe
       DetectHiddenWindows Off
       ExitApp

}

SetTimer, Closer, 1000

Closer:
Process, Exist, cmd.exe
If !ErrorLevel=0
ExitApp
Return
Last edited by t4akawolf on 30 Jul 2016, 11:33, edited 1 time in total.
Gicu
Posts: 111
Joined: 19 Aug 2014, 08:19
Location: Italy

Re: ExitApp fails to close Script when paired with Process Exist

30 Jul 2016, 04:20

checking every second if cmd is running, then exiting itself when it finds that it isn't. What am I doing wrong?

Code: Select all

If !ErrorLevel=0 ; (!) not equal 0 is 1 - that means "if cmd.exe exist exitapp"
t4akawolf
Posts: 12
Joined: 30 Jul 2016, 03:30

Re: ExitApp fails to close Script when paired with Process Exist

30 Jul 2016, 04:38

Code: Select all

If !ErrorLevel=0 ; (!) not equal 0 is 1 - that means "if cmd.exe exist exitapp"
I thought an ErrorLevel of 0 meant No Error, and that 1 meant Error = cmd doesn't exist.

EDIT: Just checked. Changing that part of the script to "If ErrorLevel=0" did not fix the issue.
Gicu
Posts: 111
Joined: 19 Aug 2014, 08:19
Location: Italy

Re: ExitApp fails to close Script when paired with Process Exist  Topic is solved

30 Jul 2016, 05:02

put

Code: Select all

SetTimer, Closer, 1000
before first return
Gicu
Posts: 111
Joined: 19 Aug 2014, 08:19
Location: Italy

Re: ExitApp fails to close Script when paired with Process Exist

30 Jul 2016, 05:08

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
 
#NoTrayIcon
#Persistent
#SingleInstance force
 
global script
 
/* Setup Tray icon and add item that will handle
* double click events
*/
Menu Tray, NoStandard
Menu Tray, Icon
Menu Tray, Icon, icon.ico
Menu Tray, Add, Show / Hide, TrayClick
Menu Tray, Add, Close, CloseItem
Menu Tray, Default, Show / Hide
 
;// Run program or batch file hidden
DetectHiddenWindows On
Run script.bat,, Hide, PID
WinWait ahk_pid %PID%
script := WinExist()
DetectHiddenWindows Off

SetTimer, Closer, 1000
return
 
TrayClick:
OnTrayClick()
return
 
;// Show / hide program or batch file on double click
OnTrayClick() {
    if DllCall("IsWindowVisible", "Ptr", script) {
        WinHide ahk_id %script%
 
    } else {
        WinShow ahk_id %script%
        WinActivate ahk_id %script%
    }
}
 
CloseItem() {
 
       DetectHiddenWindows On
       WinWait ahk_class ConsoleWindowClass
       Process, Close, cmd.exe
       DetectHiddenWindows Off
       ExitApp
 
}
  
Closer:
Process, Exist, cmd.exe
If !ErrorLevel
ExitApp
Return
t4akawolf
Posts: 12
Joined: 30 Jul 2016, 03:30

Re: ExitApp fails to close Script when paired with Process Exist

30 Jul 2016, 10:08

Gicu wrote:put

Code: Select all

SetTimer, Closer, 1000
before first return
This fixed it, thanks! :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, DataLife, ShatterCoder and 215 guests