Wait for Process

Go To StackoverFlow.com

2

In my application in VB.NET, I have started an application and introduced a wait delay of 20 seconds. But for loading the second application, the time is varying. Is it possible to have a do while loop, similar to the structure as shown below:

Start the application1
do {
    sleep(2seconds)
}until(Application1 Window is loaded
2012-04-04 21:18
by Matt


2

Did you look at Process.WaitForInputIdle()? It's waiting until the application has entered a message loop for it's user interface (created a window).

Edit: See the MSDN description on the topic here

2012-04-04 21:33
by aKzenT


1

The biggest obstacle you're going to face is how the child process communicates that it's ready. Determining if another process's window is loaded is shaky at best. It's much cleaner if you decide on something more definitive

  • Creating a particular registry key
  • Writing a value to a predetermined file
  • Windows Messages

Once you decide on that then it's fairly straight forward loop

Dim span = TimeSpan.FromSeconds(20)
Thread.Sleep(span)
Do While Not IsProcessReady()
  Thread.Sleep(span)
Loop

As said before you'll need to pick a mechanism to communicate "loaded" and that becomes your IsProcessReady function

2012-04-04 21:34
by JaredPar
This is assuming he is in control of the other application. In this case I think IPC events are a better way to handle this. Writing magic keys to the registry or a file is prone to errors in case of application crashs etc - aKzenT 2012-04-04 21:36
@aKzenT good point, added a bit about using IP - JaredPar 2012-04-04 21:42
Ads