Process Close does not close a process when performed in a SetTimer label

Posted by NbdNnm on Stack Overflow See other posts from Stack Overflow or by NbdNnm
Published on 2012-11-13T22:57:09Z Indexed on 2012/11/13 22:59 UTC
Read the original article Hit count: 411

Filed under:
|

The script below creates a child process to perform FileExist() separately from the main script so that it avoids halting the script when an unknown network path is passed.

However, the command, Process close, does not close the given process despite its ErrorLevel indicates it succeeded to close. Task Manager shows the child process still exists.

FileExistTimeout()  ; this checks if the necessary parameters are passed

; this checks if the given path exists by creating a child process with the given timeout 
PathToCheck := "\\1.2.3.4\test"
msgbox, 64, Test Result, %  FileExistTimeout(PathToCheck) "`n" ErrorLevel


FileExistTimeout(strPath="", nTimeout=1000, strSwitch="/fe") {
    global

    strParam1 = %1%
    strParam2 = %2%     

    ; if the function is used to check the script parameters
    if (strPath="") {   
        if (strParam2 = strSwitch) && strParam1         ; this means the function is placed at the beginning of the script.
            ExitApp % (FileExist(strParam1) != "") + 1  ; Return 1 or 2.
        Return
    }

    ; create a child process to check the path
    tooltip, RunWait is performing....
    SetTimer, FileExistTimeout, % -1 * nTimeout 
    RunWait "%A_AhkPath%"  "%A_ScriptFullPath%" "%strPath%" "%strSwitch%",,, nPID
    SetTimer, FileExistTimeout, Off ; Disable the timer (if it hasn't fired already).
    tooltip

    ; ErrorLevel contains the exit code of the process (from RunWait).
    if ErrorLevel = 2       ; found
        return  true
    else if ErrorLevel = 1  ; not found
        return      
    return ; timeout

    FileExistTimeout:   
        Process Close, %nPID%       ; Timed out - terminate the process.
        tooltip, % "Process Closed: " ErrorLevel "`n" "Used Pid: " nPID,,, 2
    return

}

© Stack Overflow or respective owner

Related posts about autohotkey

Related posts about file-exists