Wait for the WPF application to load after starting with Process.Start ()

I have a WinForms application that starts a wpf process using Process.Start. I would like to know when the WPF process is finished and I can access the property process.MainWindowHandle(its 0 until it is fully loaded).

I tried polling, but the descriptor is always 0. However, if I debug and wait (after Process.Start) for the WPF application to load, then I get the correct descriptor.

Does not work:

int maxCount=100000;
int count=0;
do
{
    wpfProcess.WaitForInputIdle();
    _hWnd = net4ReconProcess.MainWindowHandle;
    count++;
} while (_hWnd.ToInt32() == 0 || count > maxCount);
+2
source share
2 answers

Add process.Refresh();a while loop.

+5
source

while WaitForInputIdle , , . . , WaitForInputIdle WaitForProcessStartupComplete - The Old New Thing

, WaitForProcessStartupComplete.

:

if (!wpfProcess.WaitForInputIdle(10000)) // 10 s timout
  throw new ApplicationException("Process takes too much time to start");
_hWnd = net4ReconProcess.MainWindowHandle;
0

All Articles