diff --git a/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/VisualStudio_InProc.cs b/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/VisualStudio_InProc.cs index b3d8f8cbbaa86..fcd3edac266cd 100644 --- a/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/VisualStudio_InProc.cs +++ b/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/VisualStudio_InProc.cs @@ -56,14 +56,16 @@ public void ActivateMainWindow() var activeVisualStudioWindow = dte.ActiveWindow.HWnd; Debug.WriteLine($"DTE.ActiveWindow.HWnd = {activeVisualStudioWindow}"); - - if (activeVisualStudioWindow == IntPtr.Zero) + if (activeVisualStudioWindow != IntPtr.Zero) { - activeVisualStudioWindow = dte.MainWindow.HWnd; - Debug.WriteLine($"DTE.MainWindow.HWnd = {activeVisualStudioWindow}"); + if (IntegrationHelper.TrySetForegroundWindow(activeVisualStudioWindow)) + return; } - IntegrationHelper.SetForegroundWindow(activeVisualStudioWindow); + activeVisualStudioWindow = dte.MainWindow.HWnd; + Debug.WriteLine($"DTE.MainWindow.HWnd = {activeVisualStudioWindow}"); + if (!IntegrationHelper.TrySetForegroundWindow(activeVisualStudioWindow)) + throw new InvalidOperationException("Failed to set the foreground window."); }); public int GetErrorListErrorCount() diff --git a/src/VisualStudio/IntegrationTest/TestUtilities/IntegrationHelper.cs b/src/VisualStudio/IntegrationTest/TestUtilities/IntegrationHelper.cs index ccaf219f7993c..78ce3de5e4e8e 100644 --- a/src/VisualStudio/IntegrationTest/TestUtilities/IntegrationHelper.cs +++ b/src/VisualStudio/IntegrationTest/TestUtilities/IntegrationHelper.cs @@ -177,7 +177,7 @@ public static void KillProcess(string processName) } } - public static void SetForegroundWindow(IntPtr window) + public static bool TrySetForegroundWindow(IntPtr window) { var activeWindow = NativeMethods.GetLastActivePopup(window); activeWindow = NativeMethods.IsWindowVisible(activeWindow) ? activeWindow : window; @@ -213,9 +213,11 @@ public static void SetForegroundWindow(IntPtr window) if (!NativeMethods.SetForegroundWindow(activeWindow)) { - throw new InvalidOperationException("Failed to set the foreground window."); + return false; } } + + return true; } public static void SendInput(NativeMethods.INPUT[] inputs)