C#:Process.HasExited返回false,即使进程已终止

前端之家收集整理的这篇文章主要介绍了C#:Process.HasExited返回false,即使进程已终止前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
可能是这个问题的反面: Process.HasExited returns true even though process is running?

我在一个进程上调用了Kill(),它似乎已经退出了.但是当我测试HasExited时,我得到了错误

myProcess.Kill();

while ( !myProcess.HasExited )
{
    Thread.Sleep(1000);
}

这将持续无限期.当然,我必须更改此代码以最终停止等待,但我很好奇为什么HasExited仍然返回false,因为这个过程似乎从地图上掉了下来.

解决方法

重定向标准输出MSDN声明如下:

When standard output has been redirected to asynchronous event handlers,it is possible that output processing will not have completed when this property returns true. To ensure that asynchronous event handling has been completed,call the WaitForExit() overload that takes no parameter before checking HasExited.

无论如何,建议的解决方法应该可以做到这一点:

myProcess.Kill();
myProcess.WaitForExit();
原文链接:https://www.f2er.com/csharp/91617.html

猜你在找的C#相关文章