2010-07-14 53 views
2

我有一個WinForms應用程序,開始使用Process.Start運行的WPF過程。我想知道當WPF程序加載完成,我可以訪問process.MainWindowHandle屬性(其0之前,其完全地加載)。我試着輪詢,但句柄總是0.但是,如果我調試並等待(在Process.Start之後)爲WPF應用程序加載 - 然後我將得到正確的句柄。等待WPF應用程序與的Process.Start開始後加載()

不起作用:

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

什麼是net4ReconProcess在順便把上面的代碼? – VivekDev 2016-05-08 09:31:46

回答

5

添加process.Refresh();到while循環。

0

使用了WaitForInputIdle while循環是無感,因爲這個調用阻塞當前線程,直到其他進程已完成初始化。之後,它總是立即返回。請閱讀文章WaitForInputIdle should really be called 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;