public static async Task <bool> ActivateProcess(string processName, CancellationToken token) { var hwnd = Utilities.FindProcessWindow(processName); if (hwnd == IntPtr.Zero) { return(false); } var placement = new WinApi.WINDOWPLACEMENT(); placement.length = System.Runtime.InteropServices.Marshal.SizeOf(placement); WinApi.GetWindowPlacement(hwnd, ref placement); var showCmd = placement.showCmd switch { WinApi.SW_SHOWMAXIMIZED => WinApi.SW_SHOWMAXIMIZED, WinApi.SW_SHOWMINIMIZED => WinApi.SW_RESTORE, _ => WinApi.SW_SHOWNORMAL }; using (AttachedThreadInputScope.Create()) { await InputHelpers.TimerYield(token : token); WinApi.SetForegroundWindow(hwnd); WinApi.ShowWindow(hwnd, showCmd); await InputHelpers.TimerYield(token : token); } return(true); }