Skip to content

$cs.Sys_WaitForProcess

Waits for an already-running process to exit, checking every IntervalSec seconds up to a total of MaxWaitSec seconds. If the process isn’t running to begin with, returns immediately with no error. Important: this function always returns silently, whether the process actually exited or the timeout was simply reached — there is no return value to tell you which happened. If you need to know, check $cs.Sys_ExistProcess(...) again afterward yourself.

$cs.Sys_WaitForProcess(string processname, int MaxWaitSec, int IntervalSec)

Name of the process to wait for.

Maximum total number of seconds to wait for the process to exit.

Number of seconds to sleep between each check.

None.

Wait for a setup process to finish, then check $cs.Sys_ExistProcess afterward to tell whether it actually exited or the wait simply timed out:

Terminal window
if ($cs.Sys_ExistProcess("setup.exe"))
{
$cs.Job_WriteLog("Waiting up to 60 seconds for setup.exe to finish")
$cs.Sys_WaitForProcess("setup.exe", 60, 1)
if ($cs.Sys_ExistProcess("setup.exe"))
{
$cs.Job_WriteLog("setup.exe is still running after timeout")
}
}

Wait for a slow installer’s background helper process to finish before cleaning up temporary files:

Terminal window
$cs.Job_WriteLog("Waiting up to 300 seconds for the installer helper process to complete")
$cs.Sys_WaitForProcess("InstallerHelper", 300, 5)
if ($cs.Sys_ExistProcess("InstallerHelper"))
{
$cs.Job_WriteLog("InstallerHelper did not finish in time, forcing it closed before cleanup")
$cs.Sys_KillProcess("InstallerHelper")
}
Remove-Item -Path "$env:TEMP\InstallerHelper" -Recurse -Force -ErrorAction SilentlyContinue

$cs.Sys_ExistProcess $cs.Sys_WaitForProcessToExist $cs.Sys_KillProcess