Process.WaitForInputIdle Method (Int32)
Causes the Process component to wait the specified number of milliseconds for the associated process to enter an idle state. This overload applies only to processes with a user interface and, therefore, a message loop.
Assembly: System (in System.dll)
Parameters
- milliseconds
- Type: System.Int32
A value of 1 to MaxValue that specifies the amount of time, in milliseconds, to wait for the associated process to become idle. A value of 0 specifies an immediate return, and a value of -1 specifies an infinite wait.
Return Value
Type: System.Booleantrue if the associated process has reached an idle state; otherwise, false.
| Exception | Condition |
|---|---|
| InvalidOperationException |
The process does not have a graphical interface. -or- An unknown error occurred. The process failed to enter an idle state. -or- The process has already exited. -or- No process is associated with this Process object. |
Use WaitForInputIdle(Int32) to force the processing of your application to wait until the message loop has returned to the idle state. When a process with a user interface is executing, its message loop executes every time a Windows message is sent to the process by the operating system. The process then returns to the message loop. A process is said to be in an idle state when it is waiting for messages inside of a message loop. This state is useful, for example, when your application needs to wait for a starting process to finish creating its main window before the application communicates with that window.
If a process does not have a message loop, WaitForInputIdle(Int32) throws an InvalidOperationException.
The WaitForInputIdle(Int32) overload instructs the Process component to wait a finite amount of time for the process to become idle in the message loop. If the associated process has not become idle by the end of the interval because the loop is still processing messages, false is returned to the calling procedure.
For more information about handling events, see Consuming Events.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
The WaitForInputIdle() isn't enough to get the updated information consider the following example where even if you use the WaitForInputIdle() the return value of MainWindowHandle() in most cases is invalid or equal to zero.
In most cases there is slight delay, on my computer it's about 200ms before the class is updated but this value could be different on other computers. Consider using the alternative routine which simply queries the value until it's valid or after 5 seconds it will just abort and fail.
Dim p As Process
p = Process.Start("calc.exe")
p.WaitForInputIdle(-1)
Debug.Print(p.MainWindowHandle)
Alternative;
Dim p As Process
Dim timedOut As Integer = 0
p = Process.Start("calc.exe")
p.WaitForInputIdle()
' retry for 5 seconds
Do
System.Threading.Thread.Sleep(100)
p.Refresh()
timedOut += 100 'sleep ms
Loop Until p.MainWindowHandle.ToInt32 <> 0 Or timedOut >= 5000
Debug.Print(timedOut)
If timedOut < 5000 Then
Debug.Print(p.MainWindowHandle)
Else
Debug.Print("timed out")
End If
