加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:25:32 所属栏目:百科 来源:网络整理
导读:可能是这个问题的反面: Process.HasExited returns true even though process is running? 我在一个进程上调用了Kill(),它似乎已经退出了.但是当我测试HasExited时,我得到了错误: myProcess.Kill();while ( !myProcess.HasExited ){ Thread.Sleep(1000);}
可能是这个问题的反面: 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();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读