我正在使用 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);