可能是这个问题的反面:Process.HasExited returns true even though process is running?
我在进程上调用了 Kill(),它似乎已经退出。但是当我测试 HasExited 时,我得到了 false:
myProcess.Kill();
while ( !myProcess.HasExited )
{
Thread.Sleep(1000);
}
而且这种情况会无限期地持续下去。当然,我必须更改此代码以最终停止等待,但我很好奇为什么 HasExited 仍然返回 false,可以这么说,当进程似乎已经从 map 上掉下来时。
最佳答案
你在重定向标准输出吗? 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();
关于C#:即使进程已终止,Process.HasExited 也会返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2779653/