private IntPtr GetWindowToSetAsForeground() { IntPtr windowPtr = default; NativeMethodsHelper.EnumerateWindows(hWnd => { // Condition order important -> the VirtualDesktop call is a (potential) RPC so if we // can short circuit the check it would be better var matchedWindow = IsValidWindow(hWnd) && _client.IsWindowOnCurrentDesktop(hWnd); if (!matchedWindow) { return(true); // Carry on search } Logger.Debug(() => $"Found valid window to focus: {DumpHWndTitle(hWnd)} - { GetProcessForHWnd(hWnd) }"); windowPtr = hWnd; return(false); }); return(windowPtr); }