diff --git a/src/VisualStudio/IntegrationTest/TestUtilities/IntegrationHelper.cs b/src/VisualStudio/IntegrationTest/TestUtilities/IntegrationHelper.cs index 483a31b6972a10481f16712c9601cb02ab92439d..c3f8f6521ac43727dd42b4d26179d987ed791227 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; }