From 956f373fad477a251209dee3630a0a9a41947679 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 12 Dec 2018 15:45:58 -0600 Subject: [PATCH] Add a timeout for GetForegroundWindow --- .../IntegrationTest/TestUtilities/IntegrationHelper.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/VisualStudio/IntegrationTest/TestUtilities/IntegrationHelper.cs b/src/VisualStudio/IntegrationTest/TestUtilities/IntegrationHelper.cs index 483a31b6972..c3f8f6521ac 100644 --- a/src/VisualStudio/IntegrationTest/TestUtilities/IntegrationHelper.cs +++ b/src/VisualStudio/IntegrationTest/TestUtilities/IntegrationHelper.cs @@ -81,15 +81,18 @@ public static async Task DownloadFileAsync(string downloadUrl, string fileName) public static IntPtr GetForegroundWindow() { // 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 stopwatch = Stopwatch.StartNew(); do { foregroundWindow = NativeMethods.GetForegroundWindow(); } - while (foregroundWindow == IntPtr.Zero); + while (foregroundWindow == IntPtr.Zero && stopwatch.Elapsed < TimeSpan.FromMilliseconds(250)); return foregroundWindow; } -- GitLab