set execution timeout for batch

heyangben 81 Reputation points
2022-04-22T02:49:52.58+00:00

For example, if I execute a "start /w PKGMGR /iu:" installation command, If the execution has not been completed for more than ten minutes, a prompt will pop up. How do I set the timeout and prompt?

Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Client for IT Pros | User experience | Other
{count} votes

Answer accepted by question author
  1. MotoX80 37,151 Reputation points
    2022-04-22T12:34:37.433+00:00

    You will need to use Powershell to do that.

    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $process = Start-Process "PkgMgr.exe" -PassThru
    $StartTime = Get-Date
    Write-Host "You have 15 seconds to do something."
    while ($true) {
        if ($process.HasExited) {
            Write-Host "It ended."
            break
        }
        Start-Sleep 5 
        Write-Host "Testing."
        $ts = New-TimeSpan –Start $StartTime –End (Get-Date) 
        if ($ts.TotalSeconds -gt 15) {
            $oReturn=[System.Windows.Forms.Messagebox]::Show("What is taking so long??????")
            Write-Host "You took too long."
            $process.Kill() 
            break
        }
    }
     
    

    This site shows how to generate a prompt.

    https://michlstechblog.info/blog/powershell-show-a-messagebox/


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.