简体   繁体   中英

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 .

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. You can request or force the associated process to exit by calling CloseMainWindow or 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. 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.

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.

The time out parameter of WaitForExit only says how much time your code will wait for the process to exit. 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. 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

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 .

process.WaitForExit(10); 

returns true if the associated process has exited; 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.

as says in 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. If you wanted to see if there was an error you can check 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. 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?

You should call Kill . You should also wrap the process in a using block so the resources are disposed.

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:

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); and compare it to your time out (10s here). This way you know the exact time of running the process which is not achievelable by other methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM