为什么Process.WaitForInputIdle()不起作用?

Cur*_*tis 5 wpf process

我正在使用Windows自动化来测试我的UI并且正在打开和关闭进程.我想拥有一个有效的WindowHandle,但Process.WaitForInputIdle()不会等待足够长的时间.我有一个解决方法,但不明白为什么WaitForInputIdle()不起作用.

下面是一个小代码片段:

Process = new Process
              {
                 StartInfo =
                 {
                     WorkingDirectory = directory,
                     FileName = EXECUTABLE_FILE_NAME
                 }
              };

Process.Start();

//Process.WaitForInputIdle() doesn't work, 
//so will use a while loop until MainWindowHandle isn't IntPtr.Zero anymore,
//or until 10 seconds have elapsed

int count = 0;

while (Process.MainWindowHandle == IntPtr.Zero && count<100)
{
    count++;
    Thread.Sleep(100);
}

AppElement = AutomationElement.FromHandle(Process.MainWindowHandle);
Run Code Online (Sandbox Code Playgroud)

Cur*_*tis 2

正如 Chaser324 在他的评论中所说,我的问题的答案可以在这里找到。

我基本上需要在“while”循环内添加对 Process.Refresh() 的调用。