C# 如何知道System.Diagnostic.Process是否因超时而退出?

C# 如何知道System.Diagnostic.Process是否因超时而退出?,c#,C#,现在我有点像 void MyMethod { Process process = new Process(); process.StartInfo.FileName = "cmd.exe"; process.Start(); process.WaitForExit(10); // here we set a timeout of 10 seconds //now here I'd like to check whether the process exited n

现在我有点像

void MyMethod
{
   Process process = new Process();
   process.StartInfo.FileName = "cmd.exe";
   process.Start();
   process.WaitForExit(10); // here we set a timeout of 10 seconds

   //now here I'd like to check whether the process exited normally or
   //due to timeout. How do I do this?
   // Important: I wanna know whether it timed out or not, not if it has exited or not.
}
如何判断进程是否在超时之前退出?

您可以使用

获取一个值,该值指示关联的进程是否已终止

备注

HasExited的值为true表示关联进程已正常或异常终止。您可以通过调用CloseMainWindow或Kill请求或强制关联进程退出。如果某个句柄对该进程打开,则操作系统会在该进程退出时释放该进程内存,但会保留有关该进程的管理信息,如句柄、退出代码和退出时间。要获取此信息,可以使用ExitCode和ExitTime属性。对于此组件启动的进程,将自动填充这些属性。当与系统进程相关联的所有进程组件都被销毁并且不再持有已退出进程的句柄时,将发布管理信息

进程可以独立于代码终止。如果使用此组件启动进程,系统会自动更新HasExited的值,即使关联进程独立退出


WaitForExit的超时参数只表示代码等待进程退出的时间。但是,它不会终止进程本身


此方法(WaitForExit)指示流程组件等待有限的时间,以便流程退出。如果由于终止请求被拒绝,相关进程在间隔结束时未退出,则会将false返回给调用过程。” 资料来源:

还可以将流程包装在using块中,因为它实现了
IDisposable

process.WaitForExit(10); 
如果关联进程已退出,则返回true;否则返回false。 换句话说,如果相关联的进程在间隔结束时没有退出,则会向调用过程返回false


中所述,检查
process.WaitForExit
上的布尔结果,查看它是否由于超时而退出。如果要查看是否存在错误,可以检查

我想知道它是否超时,而不是它是否已退出

如果
process.WaitForExit
返回true,则进程将自行退出。如果返回false,则进程仍在运行且超时已过期,这(等待并返回执行)不会杀死进程


我必须手动杀掉它吗?或者只是使用一个可以杀掉它的程序来杀掉它

您应该调用。您还应该使用块将流程包装在
中,以便释放资源

void MyMethod
{
   using(Process process = new Process()) {
     process.StartInfo.FileName = "cmd.exe";
     process.Start();
     if(!process.WaitForExit(10000)) // here we set a timeout of 10 seconds (time is in milliseconds)
         process.Kill(); // if you really want to stop the process, its still running here.
   }
}

Process.WaitForExit
的返回值将告诉您进程是否在超时之前退出:

void MyMethod
{
   Process process = new Process();
   process.StartInfo.FileName = "cmd.exe";
   process.Start();
   var processExited = process.WaitForExit(10); // here we set a timeout of 10 seconds

   //now here I'd like to check whether the process exited normally or
   //due to timeout. How do I do this?
   // Important: I wanna know whether it timed out or not, not if it has exited or not.
   if (processExited)
   {
   }
}

只需跟踪进程前后的时间。WaitForExit(10);
并将其与您的超时时间(此处为10秒)进行比较。这样您就可以知道运行进程的确切时间,这是其他方法无法实现的。

因此,如果超时,它将返回false?这不是很好的解释=(如果相关进程在间隔结束时未退出,则返回false到调用过程。我尝试了此操作,但如果超时,则无论如何都将退出=(.我想知道它是否超时,而不是它是否已退出。WaitForExit的超时参数只表示代码等待进程退出的时间。但是,它不会杀死进程本身。”此方法指示流程组件等待有限的时间以使流程退出。如果由于终止请求被拒绝而导致关联流程在间隔结束时未退出,则会将false返回到调用过程。“来源:
WaitForExit(10000)
如果要等待10秒:…指示流程组件等待指定的毫秒数,以便关联的流程退出。。。“我必须手动杀死它,还是仅仅使用一次就可以杀死它以使其IDisposable?非常感谢!@Damieh-在我的回答中回答了您的上述问题。您应该调用kill,但您也应该将您的进程包装在using块中,以便在使用后将其释放。
WaitForExit(10000)
如果您想等待10秒:“…指示流程组件等待指定的毫秒数,以使关联的流程退出…”@DmitryBychenko-捕捉良好。这就是我在不检查参数的情况下复制/粘贴得到的结果。我更新了代码。非常感谢您的帮助Igor!=)
void MyMethod
{
   Process process = new Process();
   process.StartInfo.FileName = "cmd.exe";
   process.Start();
   var processExited = process.WaitForExit(10); // here we set a timeout of 10 seconds

   //now here I'd like to check whether the process exited normally or
   //due to timeout. How do I do this?
   // Important: I wanna know whether it timed out or not, not if it has exited or not.
   if (processExited)
   {
   }
}