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.

External Processes

Occasionally your .NET programs need to work with external processes. These may be managed or unmanaged processes that are already executing or that you launch yourself using the Process class's Start method. In some situations you might need to wait for such a process to exit. For example, you might launch a third-party application that performs some processing before recording its results in a text file. In order that you can read that file you would wait for that process to exit and release the file resource.

To halt execution of a program until an external process terminates you can use the WaitForExit method, which is provided by the Process class. This class is found in the System.Diagnostics namespace, so include the following using directive for simplicity:

using System.Diagnostics;

Waiting for a Process Indefinitely

The simplest variant of WaitForExit requires no parameters. When executed against a Process instance the current thread is blocked until that process exits. You should avoid executing WaitForExit from the user interface (UI) thread of an application, as the blocking of this thread is likely to make the UI unresponsive. You should perform an asynchronous operation instead.

To show the use of the method create a new console application project and add the following code to the Main method. This code launches Notepad, capturing the newly generated Process object in the 'p' variable. It then waits for the process to exit, stopping the console application until you manually close Notepad. When Notepad stops, a message is displayed in the console.

Process p = Process.Start("notepad.exe");

NB: The Close method is called after waiting to ensure that the process, which has no remaining references, has its resources correctly released.

Waiting Temporarily

Waiting indefinitely for an external process to end is not always ideal. An alternative approach is to call WaitForExit and supply a timeout. The timeout is specified using an integer argument that defines the maximum number of milliseconds to wait. If the external process terminates within this period the method returns true and the program resumes immediately. If the timeout period expires and the process is still active, the method returns false and control passes to the next statement.

We can demonstrate this with the following code, again within the Main method of a console application. Here we start Notepad and show the "Launched" message. The while loop repeatedly calls WaitForExit with a one second timeout. Each iteration outputs a full stop (period) character, giving the user some feedback. When Notepad is closed, WaitForExit returns false and the loop terminates.

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

Special Case Timeouts

The value that you provide for the timeout will usually be a positive value. If you provide a negative number the result is the same as if you did not provide an argument; the method will wait indefinitely for the external process to exit. If you specify a zero timeout the method will return immediately with a result of true, if the process has already exited, or false, if it is still running.

15 November 2012