简体   繁体   English

如何知道是否已System.Diagnostic.Process由于退出超时?

[英]How to know if a System.Diagnostic.Process has exited due to timeout?

Right now I have something like 现在我有类似

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.
}

How can I tell if the process exited before the timeout? 我如何知道该进程是否在超时之前退出?

You can use Process.HasExited Property . 您可以使用Process.HasExited属性

Gets a value indicating whether the associated process has been terminated. 获取一个值,该值指示关联的进程是否已终止。

Remarks 备注

A value of true for HasExited indicates that the associated process has terminated, either normally or abnormally. HasExited的值为true表示关联的进程已正常或异常终止。 You can request or force the associated process to exit by calling CloseMainWindow or Kill. 您可以通过调用CloseMainWindow或Kill来请求或强制关联的进程退出。 If a handle is open to the process, the operating system releases the process memory when the process has exited, but retains administrative information about the process, such as the handle, exit code, and exit time. 如果进程打开了句柄,则操作系统将在进程退出时释放进程内存,但会保留有关进程的管理信息,例如句柄,退出代码和退出时间。 To get this information, you can use the ExitCode and ExitTime properties. 要获取此信息,可以使用ExitCode和ExitTime属性。 These properties are populated automatically for processes that were started by this component. 对于由该组件启动的进程,将自动填充这些属性。 The administrative information is released when all the Process components that are associated with the system process are destroyed and hold no more handles to the exited process. 当与系统进程相关联的所有Process组件都被销毁并且不再保存已存在进程的句柄时,将释放管理信息。

A process can terminate independently of your code. 进程可以独立于您的代码而终止。 If you started the process using this component, the system updates the value of HasExited automatically, even if the associated process exits independently. 如果使用此组件启动了流程,则即使关联的流程独立退出,系统也会自动更新HasExited的值。

The time out parameter of WaitForExit only says how much time your code will wait for the process to exit. WaitForExit的timeout参数仅表示您的代码等待进程退出的时间。 However, it will not kill the process itself. 但是,它不会终止进程本身。

This method (WaitForExit) instructs the Process component to wait a finite amount of time for the process to exit. 此方法(WaitForExit)指示Process组件等待有限的时间以使进程退出。 If the associated process does not exit by the end of the interval because the request to terminate is denied, false is returned to the calling procedure." Source: http://msdn.microsoft.com/en-us/library/ty0d8k56.aspx 如果由于终止请求被拒绝而在间隔结束之前关联的进程没有退出,则将错误返回给调用过程。”来源: http//msdn.microsoft.com/zh-cn/library/ty0d8k56。 aspx

void MyMethod
{
    using (var 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.
        if (!process.HasExited)
        {
            // Do something.
        }
    }
}

Also wrap the process in a using block, as it implements IDisposable . 还将过程包装在using块中,因为它实现了IDisposable

process.WaitForExit(10); 

returns true if the associated process has exited; 如果关联的进程已退出,则返回true; otherwise, false. 否则为假。 In other words, if the associated process does not exit by the end of the interval, false is returned to the calling procedure. 换句话说,如果关联的进程在间隔结束之前没有退出,则将false返回给调用过程。

as says in https://msdn.microsoft.com/ru-ru/library/ty0d8k56(v=vs.110).aspx https://msdn.microsoft.com/ru-ru/library/ty0d8k56(v=vs.110).aspx中所述

Check the boolean result on process.WaitForExit to see if it was exited due to just the time out. 检查process.WaitForExit上的布尔结果,看看是否只是由于超时而退出了。 If you wanted to see if there was an error you can check Process.ExitCode . 如果要查看是否有错误,可以检查Process.ExitCode

I wanna know whether it timed out or not, not if it has exited or not. 我想知道是否超时与否,如果它已退出与否。

If process.WaitForExit returns true the process exited on its own. 如果process.WaitForExit返回true,则进程自行退出。 If false the process is still running and the timeout has expired, this (the waiting and return to execution) does not kill the process. 如果假进程仍在运行和超时已过期,这(在等待和恢复执行) 杀的过程。

Do I have to kill it manually or just doing a using will kill it do to IDisposable? 难道我必须手动杀死它,还是做一个使用会杀死它做了IDisposable?

You should call Kill . 你应该叫 You should also wrap the process in a using block so the resources are disposed. 你也应该在包装过程中using块这样的资源配置。

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.
   }
}

The return value of Process.WaitForExit will tell you whether the process exited before the timeout: 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)
   {
   }
}

Just keep track of time before and after process.WaitForExit(10); 只需跟踪处理前后的时间即可process.WaitForExit(10); and compare it to your time out (10s here). 并将其与您的超时时间进行比较(此处为10秒)。 This way you know the exact time of running the process which is not achievelable by other methods. 这样,您可以知道运行该过程的确切时间,而其他方法是无法实现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Diagnostic.Process中的死锁 - Deadlock in System.Diagnostic.Process 无法使用System.Diagnostic.Process运行nbtstat - Unable to run nbtstat using System.Diagnostic.Process 获取密码提示C#System.Diagnostic.Process的通知 - Getting notified of password prompt C# System.Diagnostic.Process 如何抑制使用System.Diagnostic.Process执行的exe的对话框? - How do I supress dialogs from an exe that is executed using System.Diagnostic.Process? 什么是WinRT(C#)上的System.Diagnostic.Process的等价物? - What's the equivalent of the System.Diagnostic.Process on WinRT (C#)? 我可以捕获用C#的System.Diagnostic.Process()启动的命令行应用程序的APPCRASH吗? - Can I catch APPCRASH of command line app launched with C#'s System.Diagnostic.Process()? 在C#中使用System.Diagnostic.Process及其UninstallString卸载程序 - Uninstall program using System.Diagnostic.Process and its UninstallString in C# 如何确定流程是否已启动但尚未退出? - How to determine if a Process has started but not yet exited? 如何在没有“进程退出”异常的情况下终止进程? - How to kill a process without getting a “process has exited” exception? 异步使用System.Diagnostics.Process,在确定退出之前,我应该如何确保收到最后一个输出? - Using System.Diagnostics.Process asynchronously, how should I ensure that I've received the last output before determining it has exited?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM