System.Diagnostics.Process.WaitForExit Method

Instructs the System.Diagnostics.Process component to wait indefinitely for the associated process to exit.

Syntax

public void WaitForExit ()

Remarks

The Process.WaitForExit overload is used to make the current thread wait until the associated process terminates. This method instructs the System.Diagnostics.Process component to wait an infinite amount of time for the process and event handlers to exit. This can cause an application to stop responding. For example, if you call Process.CloseMainWindow for a process that has a user interface, the request to the operating system to terminate the associated process might not be handled if the process is written to never enter its message loop.

Note:

In the net_v35_long and earlier versions, the Process.WaitForExit overload waited for int.MaxValue milliseconds (approximately 24 days), not indefinitely. Also, previous versions did not wait for the event handlers to exit if the full int.MaxValue time was reached.

This overload ensures that all processing has been completed, including the handling of asynchronous events for redirected standard output. You should use this overload after a call to the Process.WaitForExit(int) overload when standard output has been redirected to asynchronous event handlers.

When an associated process exits (that is, when it is shut down by the operation system through a normal or abnormal termination), the system stores administrative information about the process and returns to the component that had called Process.WaitForExit. The System.Diagnostics.Process component can then access the information, which includes the Process.ExitTime, by using the Process.Handle to the exited process.

Because the associated process has exited, the Process.Handle property of the component no longer points to an existing process resource. Instead, the handle can be used only to access the operating system's information about the process resource. The system is aware of handles to exited processes that have not been released by System.Diagnostics.Process components, so it keeps the Process.ExitTime and Process.Handle information in memory until the System.Diagnostics.Process component specifically frees the resources. For this reason, any time you call Process.Start for a System.Diagnostics.Process instance, call Process.Close when the associated process has terminated and you no longer need any administrative information about it. Process.Close frees the memory allocated to the exited process.

Requirements

Namespace: System.Diagnostics
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0