3

I have a snippet of code which basically invokes an application using Process.Start() method.

ProcessStartInfo psi = new ProcessStartInfo(strAppPath);
psi.WindowStyle = ProcessWindowStyle.Maximized | ProcessWindowStyle.Normal;
//Starts the xyz application process.
Process xyzProcess = Process.Start(psi);
xyzProcess.WaitForInputIdle();

Although the process is started within few seconds, the application can take some amount of time to initialize completely. i.e my application xyz can take 5 - 10 seconds depending on the machine it is running on (i.e less than 5 secs on a faster pc and >10 secs on slower pc's)

My question : Is there a way to track this ? By polling some property apart from waiting for sometime using Thread.Sleep(ms).

I tried using the following approches in vain

  1. Polling xyzProcess.Responding property with a small thread.sleep

  2. Dirty approach of polling xyzProcess.MainWindowTitle till it returns some non-null value.

  3. xyzProcess.WaitForInputIdle(sleeptime);

Please help

Thanks

superM
  • 8,605
  • 8
  • 42
  • 51
this-Me
  • 2,139
  • 6
  • 43
  • 70
  • Do you own the 2nd application? – Adriano Repetti Jun 15 '12 at 12:42
  • Unfortunately **NO**. Else i could've raised an event upon the initialization complete. – this-Me Jun 15 '12 at 12:44
  • You already do with WaitForInputIdle(). There is no other good way. You certainly don't want to call it with a timeout, that can only tell it when it is *not* ready. – Hans Passant Jun 15 '12 at 12:49
  • Can you make any assertion about its behavior? For example if it'll respond to user input during initialization or if it'll ignore all Windows messages or something like that. If you do not know anything about the application then it'll be hard to understand when it's ready (actually if something in its UI changes you may use Spy++ to get IDs and GetChildWindows() to monitor for that changes) – Adriano Repetti Jun 15 '12 at 12:49
  • It cannot be done in a good way. There are interprocess communcation tools for such reasons. In your case you could only do some tricks according to second app behavior. Please describe startup behavior of the app – Anton Semenov Jun 15 '12 at 12:54
  • @HansPassant : I just use it with an infinite timeout (no parameter being passed to the WaitForInputIdle). – this-Me Jun 15 '12 at 13:00
  • I tried this approach by starting the application indirectly via command prompt. Then starting the CMD.exe through Process.Start(). I see that this approach is slightly faster than doing it as mentioned earlier. Anybody knows why this is so ? – this-Me Jun 16 '12 at 05:54
  • How actually did you know when the application is initialized? – Nicolae Dascalu Jun 16 '12 at 13:50
  • @NicolaeDascalu : I still do not know when the 2nd app is initialized and ready, but i just made this observation visually. – this-Me Jun 17 '12 at 18:23
  • And what did you visually observed? This is the key... – Nicolae Dascalu Jun 18 '12 at 09:19
  • @NicolaeDascalu : Basically when the UI is initialized and when the application is ready, it begins to pump in the data to a particular port. My application reads this data through sockets. At present i've implemented this in a **dirty** way.i.e Listen to the port till its available – this-Me Jun 25 '12 at 10:29
  • @this-Me Seams that you handled ok (in your case) ... – Nicolae Dascalu Jun 25 '12 at 12:45

1 Answers1

0

Are you just waiting for the application initialize, or are you just waiting for the application to do something?

If it's just that the application needs to "do something" then can you not just wait for exit?

xyzProcess.WaitForExit();
Mo D
  • 559
  • 1
  • 6
  • 15