Редагувати

Поділитися через


Process.WaitForExit Method

Definition

Sets the period of time to wait for the associated process to exit, and blocks the current thread of execution until the time has elapsed or the process has exited. To avoid blocking the current thread, use the Exited event.

For code examples, see the StandardError and the ExitCode property reference pages.

Overloads

WaitForExit()

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

WaitForExit(Int32)

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

WaitForExit(TimeSpan)

Instructs the Process component to wait the specified amount of time for the associated process to exit.

WaitForExit()

Source:
Process.cs
Source:
Process.cs
Source:
Process.cs

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

public:
 void WaitForExit();
public void WaitForExit ();
member this.WaitForExit : unit -> unit
Public Sub WaitForExit ()

Exceptions

The wait setting could not be accessed.

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.

Examples

See the Remarks section of the StandardError property reference page.

Remarks

WaitForExit() makes the current thread wait until the associated process terminates. It should be called after all other methods are called on the process. To avoid blocking the current thread, use the Exited event.

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.

Note

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.

See also

Applies to

WaitForExit(Int32)

Source:
Process.cs
Source:
Process.cs
Source:
Process.cs

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

public:
 bool WaitForExit(int milliseconds);
public bool WaitForExit (int milliseconds);
member this.WaitForExit : int -> bool
Public Function WaitForExit (milliseconds As Integer) As Boolean

Parameters

milliseconds
Int32

The amount of time, in milliseconds, to wait for the associated process to exit. A value of 0 specifies an immediate return, and a value of -1 specifies an infinite wait.

Returns

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

Exceptions

The wait setting could not be accessed.

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(Int32) for a process that is running on a remote computer. This method is available only for processes that are running on the local computer.

milliseconds is a negative number other than -1, which represents an infinite time-out.

Examples

See the code example for the ExitCode property.

Remarks

WaitForExit(Int32) makes the current thread wait until the associated process terminates. It should be called after all other methods are called on the process. To avoid blocking the current thread, use the Exited event.

This method instructs the 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 Timeout.Infinite for milliseconds, and Process.WaitForExit(Int32) will behave the same as the 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 Framework 3.5 and earlier versions, if milliseconds was -1, the WaitForExit(Int32) overload waited for 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 WaitForExit() overload that takes no parameter after receiving a true from this overload. To help ensure that the Exited event is handled correctly in Windows Forms applications, set the 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 WaitForExit(Int32). 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.

See also

Applies to

WaitForExit(TimeSpan)

Source:
Process.cs
Source:
Process.cs
Source:
Process.cs

Instructs the Process component to wait the specified amount of time for the associated process to exit.

public:
 bool WaitForExit(TimeSpan timeout);
public bool WaitForExit (TimeSpan timeout);
member this.WaitForExit : TimeSpan -> bool
Public Function WaitForExit (timeout As TimeSpan) As Boolean

Parameters

timeout
TimeSpan

The amount of time to wait for the associated process to exit.

Returns

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

Remarks

See WaitForExit(Int32) remarks.

Applies to