Process.WaitForExit 方法

1.是指示 Process 组件无限期地等待关联进程退出。
2.WaitForExit 重载用于使当前线程处于等待状态,直到关联的进程终止。 此方法指示 Process 组件无限期地等待该进程和事件处理程序退出。 这可能会导致应用程序停止响应。 例如,如果对有用户界面的进程调用 CloseMainWindow,并且该进程被编写为从不进入其消息循环,则可能不处理向操作系统发出的终止关联进程的请求。
3.注意
在 .NET Framework 3.5 版 和早期版本中,WaitForExit 重载会等待 MaxValue 毫秒(约 24 天),并不是无限期的。 此外,如果达到充分的 MaxValue 时间,则以前的版本不会等待事件处理程序退出。
4.参考
http://msdn.microsoft.com/zh-cn/library/fb4aw7b8(v=VS.100).aspx
try { # 启动 cmd.exe 并重定向输出到临时文件 # $process | Tee-Object -FilePath $outFile -Append # Write-Host "信息" *>> $outFile $process = [System.Diagnostics.Process]::New() $process.StartInfo.FileName = "$exePath" $process.StartInfo.Arguments = "$arguments" $process.StartInfo.RedirectStandardOutput = $true $process.StartInfo.RedirectStandardError = $true $process.StartInfo.UseShellExecute = $false # 必须禁用以启用重定向 # 同步读取(阻塞直到进程结束) $process.Start() | Out-Null Log-Message "process.Start" $stdout = $process.StandardOutput.ReadToEnd() # $stderr = $process.StandardError.ReadToEnd() $process.WaitForExit() Write-Output "$stdout" | Tee-Object -FilePath $outFile -Append # Write-Warning "STDERR: $stderr" | Tee-Object -FilePath $outFile -Append # 超时监控循环 $timer = [Diagnostics.Stopwatch]::StartNew() $processHasExited = $false Log-Message "super watch Start" while (-not $processHasExited) { if ($timer.Elapsed.TotalSeconds -ge $timeoutSeconds) { Write-Host "`n【超时强制终止】" -ForegroundColor Red $process.Kill() # 终止主进程 break } # 检查进程状态(间隔 200ms) Start-Sleep -Milliseconds 100 $processHasExited = $process.HasExited } # 获取退出码 $exitCode = $process.ExitCode Write-Host "`n【执行完成】退出码: $exitCode" -ForegroundColor Yellow } catch { Write-Host "`n【错误】$_" -ForegroundColor Red } finally { # 清理临时文件 if (Test-Path $tempFile) { Remove-Item $tempFile } }无法进入超级监控循环
最新发布
04-01
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值