System.Diagnostics.Process.WaitForExit Method

Instructs the System.Diagnostics.Process component to wait the specified number of milliseconds for the associated process to exit.

Syntax

public bool WaitForExit (int milliseconds)

Parameters

milliseconds
The amount of time, in milliseconds, to wait for the associated process to exit. The maximum is the largest possible value of a 32-bit integer, which represents infinity to the operating system.

Returns

true if the associated process has exited; otherwise, false.

Remarks

The Process.WaitForExit(int) overload is used to make the current thread wait until the associated process terminates. This overload instructs the System.Diagnostics.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. You can specify a negative number (System.Threading.Timeout.Infinite) for milliseconds, and Process.WaitForExit(int) will behave the same as the Process.WaitForExit overload. If you pass 0 (zero) to the method, it returns true only if the process has already exited; otherwise, it immediately returns false.

Note:

In the net_v35_long and earlier versions, if milliseconds was -1, the Process.WaitForExit(int) overload waited for int.MaxValue milliseconds (approximately 24 days), not indefinitely.

When standard output has been redirected to asynchronous event handlers, it is possible that output processing will not have completed when this method returns. To ensure that asynchronous event handling has been completed, call the Process.WaitForExit overload that takes no parameter after receiving a true from this overload. To help ensure that the Process.Exited event is handled correctly in Windows Forms applications, set the Process.SynchronizingObject property.

When an associated process exits (is shut down by the operating 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(int). 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