RunDependent()

Post your working scripts, libraries and tools for AHK v1.1 and older
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

RunDependent()

17 Dec 2018, 13:27

A wrapper for the Run command,when script exits, all child processes, that were run by script are terminated as well,regardless of how the script is terminated....

Intended as a garbage collector for script child processes.
Compatible with compiled scripts.

I welcome any suggestions for improvements or better approaches...

@Iseahound ,Thanks for the Suggestions & Snippets...

Code: Select all

MsgBox When the script exists...`n all child processes currently alive also die...`n Close Calculator To Exit Script.
RunDependent("wordpad.exe")
RunDependent("mspaint.exe")
RunDependent("calc.exe",,,true)	;RunWait calculator,... when closed,script exists & all other child processes should do so as well.


;Garbage Collector For Script Child Processes, All Of Which Are Killed If Script Exists For Any Reason...
RunDependent(target, workingdir:="", options:="", RunWait:=false){
	Try{
		If !RunWait
			Run, % target, % workingdir, % options, cPid
		Else
			RunWait, % target, % workingdir, % options, cPid
	}Catch
		Return
	sPid := DllCall("GetCurrentProcessId"), q := Chr(0x22)	;q = quote char
	If !A_IsCompiled{
		childMonitor := "Process Exist," sPid . "`nWhile ErrorLevel" . "`n{" . "`nsleep 100" . "`nProcess Exist," cPid . "`nIf !ErrorLevel" . "`nBreak" . "`nProcess Exist," sPid . "`n}" . "`nProcess Close," cPid
		,shell := ComObjCreate("WScript.Shell"),exec := shell.Exec("AutoHotkey.exe /ErrorStdOut *"),exec.StdIn.Write(childMonitor),exec.StdIn.Close()
	}Else{
		_exit := Comspec " /q /c for /L %n in (1,0,10) do (timeout /t 1 1>NUL && (tasklist /FI " q "PID eq "
		. sPid q " 2>NUL | find /I /N " q sPid  q " 1>NUL || TASKKILL /PID "
		. cPid " /F 2>NUL) & (tasklist /FI " q "PID eq " cPid q " 2>NUL | find /I /N " q cPid q " 1>NUL || exit))"
		Run % _exit,,Hide
	}
	Return cPid
}



Last edited by CyL0N on 18 Dec 2018, 01:01, edited 1 time in total.
live ? long & prosper : regards
iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: RunDependent()

17 Dec 2018, 22:05

Powershell auxiliary code:

Code: Select all

      static q := Chr(0x22) ; Escape double quote character.
      ; Get name of current process.
      VarSetCapacity(_process, 2048)
      DllCall("GetModuleFileName", "int", 0, "str", _process)
      SplitPath, _process,,,, _process
      _exit := "powershell -NoProfile -command " q "& {Do {if (Get-Process -id " DllCall("GetCurrentProcessId")
               . " | where {$_.Processname -eq '" _process "'}) {sleep 1} else {Get-Process -id " _pid
               . " | foreach {$_.CloseMainWindow(); Stop-Process -id $_.id}}} while (Get-Process -id " _pid ")}" q
      Run, % _exit,, Hide
cmd auxiliary code:

Code: Select all

      static q := Chr(0x22) ; Escape double quote character.
      ; Get name of current process.
      VarSetCapacity(_process, 2048)
      DllCall("GetModuleFileName", "int", 0, "str", _process)
      SplitPath, _process,,,, _process
      _exit := Comspec " /q /c for /L %n in (1,0,10) do (timeout /t 1 1>NUL && (tasklist /FI " q "PID eq "
            . DllCall("GetCurrentProcessId") q " 2>NUL | find /I /N " q _process q " 1>NUL || TASKKILL /PID "
            . _pid " /F 2>NUL) & (tasklist /FI " q "PID eq " _pid q " 2>NUL | find /I /N " q _pid q " 1>NUL || exit))"
      Run, % _exit,, Hide
as alternatives to your ExecScript() where _pid is childPID from https://autohotkey.com/boards/viewtopic.php?t=31273

Code: Select all

	childMonitor := "
	(LTrim
	Process Exist, %scriptPID%
	while ErrorLevel {	;hold terminating child while parent script is alive...
		Sleep 100
		Process Exist, %childPID%
		If !ErrorLevel	;if child is no longer alive,exit...
			Break
		Process Exist, %scriptPID%
	}
	Process Close, %childPID%
	)"

	ExecScript(childMonitor, false)
One less dependent function d:
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: RunDependent()

18 Dec 2018, 01:03

iseahound wrote:
17 Dec 2018, 22:05
Thanks Dude, updated my OP Accordingly.
live ? long & prosper : regards

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: KruschenZ and 53 guests