Matt
Matt

Reputation: 179

Wait for Process

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

Upvotes: 2

Views: 1847

Answers (2)

JaredPar
JaredPar

Reputation: 754565

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

Upvotes: 1

aKzenT
aKzenT

Reputation: 7895

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

Upvotes: 2

Related Questions