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

时间:2016-03-28 14:07:53

标签: 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 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.
}

如何判断进程是否在超时前退出?

5 个答案:

答案 0 :(得分:2)

您可以使用Process.HasExited Property

  

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

     

<强>说明

     

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

     

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

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

  

此方法(WaitForExit)指示Process组件等待一段有限的时间让进程退出。如果关联进程在间隔结束时没有退出,因为拒绝终止请求,则返回false到调用过程。&#34;   资料来源:http://msdn.microsoft.com/en-us/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.
        }
    }
}

将进程包装在using块中,因为它实现了IDisposable

答案 1 :(得分:1)

      <form novalidate (ngSubmit)="registerStore()" [ngFormModel]="storeRegistrationForm">
            <div class="col-xs-12 col-sm-6">
                <div class="form-group">
                    <label>{{text.label.country}}</label>
                    <select class="form-control" ngControl="userCountryId">
                        <option *ngFor="#country of countries" [value]="country.id">{{country.name}}</option>
                    </select>
                </div>
            </div>
            <div class="col-xs-12 col-sm-6">
                <div class="form-group">
                    <label>{{text.label.storeCountry}}</label>
                    <select class="form-control" ngControl="storeCountryId">
                        <option *ngFor="#country of countries" [value]="country.id">{{country.name}}</option>
                    </select>
                </div>
            </div>
            <div class="col-xs-12 text-right">
                <button type="submit" class="btn btn-primary">
                    <span>{{text.label.finish}}</span>
                </button>
            </div>
        </form>
如果关联的进程已退出,

返回true;否则,错误。 换句话说,如果关联的进程在时间间隔结束时没有退出,则返回false到调用过程。

https://msdn.microsoft.com/ru-ru/library/ty0d8k56(v=vs.110).aspx

中所述

答案 2 :(得分:1)

检查process.WaitForExit上的布尔结果,看看它是否因超时而退出。如果您想查看是否有错误,可以查看Process.ExitCode

  

我想知道它是否超时,不管它是否已经退出。

如果process.WaitForExit返回true,则该过程自行退出。如果为false,则进程仍在运行且超时已到期,此(等待并返回执行)会终止该进程。

  

我是否必须手动杀死它或只是使用它会杀死它对IDisposable?

你应该致电Kill。您还应该将进程包装在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.
   }
}

答案 3 :(得分:0)

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)
   {
   }
}

答案 4 :(得分:0)

只需跟踪process.WaitForExit(10);之前和之后的时间,并将其与您的超时时间(此处为10秒)进行比较。通过这种方式,您可以了解运行该过程的确切时间,这是其他方法无法实现的。

相关问题