提交 956f373f 编写于 作者: S Sam Harwell

Add a timeout for GetForegroundWindow

上级 29950113
...@@ -81,15 +81,18 @@ public static async Task DownloadFileAsync(string downloadUrl, string fileName) ...@@ -81,15 +81,18 @@ public static async Task DownloadFileAsync(string downloadUrl, string fileName)
public static IntPtr GetForegroundWindow() public static IntPtr GetForegroundWindow()
{ {
// Attempt to get the foreground window in a loop, as the NativeMethods function can return IntPtr.Zero // Attempt to get the foreground window in a loop, as the NativeMethods function can return IntPtr.Zero
// in certain circumstances, such as when a window is losing activation. // in certain circumstances, such as when a window is losing activation. If no foreground window is
// identified after a short timeout, none is returned. This only impacts the ability of the test to restore
// focus to a previous window, which is fine.
var foregroundWindow = IntPtr.Zero; var foregroundWindow = IntPtr.Zero;
var stopwatch = Stopwatch.StartNew();
do do
{ {
foregroundWindow = NativeMethods.GetForegroundWindow(); foregroundWindow = NativeMethods.GetForegroundWindow();
} }
while (foregroundWindow == IntPtr.Zero); while (foregroundWindow == IntPtr.Zero && stopwatch.Elapsed < TimeSpan.FromMilliseconds(250));
return foregroundWindow; return foregroundWindow;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册