Waits until the specified process is in an idle state. Idle is defined as the state where the process is ready to process a message but no message is pending.
Code:
Const INFINITE As Int32 = -1
Const WAIT_TIMEOUT As Int32 = 258
Private Declare Function apiWaitForInputIdle Lib "user32" Alias "WaitForInputIdle" (ByVal hProcess As Int32, ByVal dwMilliseconds As Int32) As Int32
Public Function WindowWaitForIdle(ByVal hWnd As Int32, Optional ByVal dwMilliseconds As Int32 = 5000) As Boolean
Try ''''''''''''''''''''''''''''''''''''''''''''In case handle is invalid
For Each p As Process In Process.GetProcesses() 'Iterate through all processes
If p.MainWindowHandle.ToInt32 = hWnd Then 'If the handle matches the specified handle
'p.WaitForInputIdle(dwMilliseconds)'You could do this, but it may not always wait long enough like the API does
WindowWaitForIdle = CBool(apiWaitForInputIdle(p.Handle.ToInt32, dwMilliseconds)) 'Set return value
End If
Next
Catch ex As Exception
End Try ''''''''''''''''''''''''''''''''''''''''Ignore any error due to handles with no graphical interface
Return Not WindowWaitForIdle '''''''''''''''''''Return the inverse of API call, which is zero for success instead
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = WindowWaitForIdle(Me.Handle.ToInt32, 5000).ToString
End Sub