进程的 InvalidOperationException [英] InvalidOperationException with Process

查看:98
本文介绍了进程的 InvalidOperationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码开始一个新进程:

I'm starting a new process using the following code:

Process p = new Process();
p.StartInfo.FileName = "...";
p.StartInfo.Arguments = "...";
p.Start();
p.WaitForExit(300000); // 5 minutes

if (!p.HasExited) 
    p.Kill();
Console.Write(p.ExitCode);

当该过程在 5 分钟内结束时,这是有效的,但如果没有,我得到

When the process ends within the 5 minutes, that's working, but when it doesn't, I get

InvalidOperationException(进程必须在请求之前退出信息可以确定...)

InvalidOperationException (Process must exit before requested information can be determined...).

知道为什么我会收到此异常吗?

Any idea why I'm getting this exception?

谢谢.

推荐答案

根据 MSDNKill方法异步执行,调用Kill方法后,调用WaitForExit方法等待进程退出,或检查 HasExited 属性以确定进程是否已退出."

According to MSDN, "The Kill method executes asynchronously. After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine if the process has exited."

换句话说,仅仅因为 Kill 返回并不意味着该过程实际上已经消失了.您需要调用 WaitForExit 等待进程真正消失.

In other words, just because Kill returns doesn't mean the process is actually gone. You'll need to call WaitForExit to wait until the process has actually disappeared.

这篇关于进程的 InvalidOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆