Export (0) Print
Expand All

Process.WaitForExit Method

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

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)

'Declaration
Public Sub WaitForExit

ExceptionCondition
Win32Exception

The wait setting could not be accessed.

SystemException

No process Id has been set, and a Handle from which the Id property can be determined does not exist.

-or-

There is no process associated with this Process object.

-or-

You are attempting to call WaitForExit for a process that is running on a remote computer. This method is available only for processes that are running on the local computer.

The WaitForExit overload is used to make the current thread wait until the associated process terminates. This method instructs the 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 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.

NoteNote

In the .NET Framework 3.5 and earlier versions, the WaitForExit overload waited for MaxValue milliseconds (approximately 24 days), not indefinitely. Also, previous versions did not wait for the event handlers to exit if the full 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 WaitForExit(Int32) 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 WaitForExit. The Process component can then access the information, which includes the ExitTime, by using the Handle to the exited process.

Because the associated process has exited, the 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 Process components, so it keeps the ExitTime and Handle information in memory until the Process component specifically frees the resources. For this reason, any time you call Start for a Process instance, call Close when the associated process has terminated and you no longer need any administrative information about it. Close frees the memory allocated to the exited process.

For an example of how to use the WaitForExit method, see the Remarks section of the StandardError property reference page.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

  • LinkDemand 

    for full trust for the immediate caller. This member cannot be used by partially trusted code.

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft