This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 2.0+

Waiting for a Process to Exit

.NET applications sometimes need to work with external processes. In some cases it is necessary to wait for those processes to generate results and exit before the .NET software continues executing. Waiting in this way is possible using the Process class.

Process Information

Once a process has exited, the Process instance's properties are updated to provide administrative information. You can access this information until you call the object's Close method. The properties include:

  • ExitCode. This returns an integer value that describes the status of the external process on exit. The value is usually zero if the program exited without error. Other values are often indicative of specific problems. The exit codes are defined by the external software.
  • ExitTime. This DateTime value holds the date and time at which the process stopped.

Some other properties are available before the process exits but are of interest after closing. These are:

  • StartTime. The date and time at which the process was started. This can be combined with the ExitTime property to determine the duration for which the program was running.
  • TotalProcessorTime. A TimeSpan holding the amount of processor time used by the process. This does not measure time that the application was open but performing no activities. It includes time spent within operating system functions that were initiated by the process.
  • UserProcessorTime. A TimeSpan holding the amount of processor time used by the process, excluding time spent calling core operating system functions.

The following code modifies the previous sample to show the values from these properties when you exit Notepad.

Process p = Process.Start("notepad.exe");
while (!p.WaitForExit(1000))

Console.WriteLine("Exit Code:  {0}", p.ExitCode);
Console.WriteLine("Started:    {0}", p.StartTime);
Console.WriteLine("Exited:     {0}", p.ExitTime);
Console.WriteLine("Total Time: {0}", p.TotalProcessorTime);
Console.WriteLine("User Time:  {0}", p.UserProcessorTime);



The parameterless version of WaitForExit behaves slightly differently than the overload that includes a timeout value. For the former, when the external process exits, the WaitForExit method waits for all processing to complete before continuing. This includes asynchronous operations of the targeted process, such as event handler execution when the standard output has been redirected. When passing a timeout, these asynchronous operations may not be complete when WaitForExit returns. When this can be problematic, you should call the parameterless method after the version with a timeout returns true.

When using the .NET framework 4.0 or later versions, the parameterless overload of WaitForExit will wait indefinitely. In earlier .NET releases there is a timeout, albeit a very long one. The method will wait for a maximum of 231 milliseconds, which is approximately 25 days. If this duration is exceeded, the method will return.

15 November 2012