From e3f5fba870039eeb98483852b9e2d9591809541a Mon Sep 17 00:00:00 2001 From: Vaibhav Sharma <48472541+GhostVaibhav@users.noreply.github.com> Date: Sun, 9 Jun 2024 02:48:13 +0530 Subject: [PATCH] [PTRun][WindowWalker]Don't hang when closing an unresponsive window (#33167) * add(windowWalkerComponents): added a "Responding" variable signifying if a process is responding or not * add(win32NativeMethod): added "SendMessageTimeout" method in the common Win32 namespace * refactor(windowWalkerWindow): added an implementation of the helper function for closing the window using the newly defined method refactor(windowWalkerWindow): added a thread to run, whenever "CloseThisWindow" is called * refactor(resultHelper): used localizable strings for printing the responding text add(resources): added "Not Responding" localizable text to the resources file * refactor(resultHelper): refactored the message in case of a "Not Responding" task * refactor(resultHelper): modified the formatting of the subtitle * refactor(window): refactored the helper function and removed the unnecessary variable * refactor(windowProcess): changed the variable name from "Responding" to "IsResponding" * add: added try-catch to isResponding getter --- .../Components/ResultHelper.cs | 8 ++++++- .../Components/Window.cs | 12 +++++++++- .../Components/WindowProcess.cs | 24 +++++++++++++++++++ .../Properties/Resources.Designer.cs | 9 +++++++ .../Properties/Resources.resx | 3 +++ .../Wox.Plugin/Common/Win32/NativeMethods.cs | 3 +++ 6 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/ResultHelper.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/ResultHelper.cs index 827ea910ab..17a9ef4e19 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/ResultHelper.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/ResultHelper.cs @@ -92,6 +92,11 @@ namespace Microsoft.Plugin.WindowWalker.Components subtitleText += $" ({window.Process.ProcessID})"; } + if (!window.Process.IsResponding) + { + subtitleText += $" [{Resources.wox_plugin_windowwalker_NotResponding}]"; + } + if (WindowWalkerSettings.Instance.SubtitleShowDesktopName && Main.VirtualDesktopHelperInstance.GetDesktopCount() > 1) { subtitleText += $" - {Resources.wox_plugin_windowwalker_Desktop}: {window.Desktop.Name}"; @@ -148,7 +153,8 @@ namespace Microsoft.Plugin.WindowWalker.Components $"Desktop number: {window.Desktop.Number}\n" + $"Desktop is visible: {window.Desktop.IsVisible}\n" + $"Desktop position: {window.Desktop.Position}\n" + - $"Is AllDesktops view: {window.Desktop.IsAllDesktopsView}"; + $"Is AllDesktops view: {window.Desktop.IsAllDesktopsView}\n" + + $"Responding: {window.Process.IsResponding}"; return new ToolTipData(window.Title, text); } diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/Window.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/Window.cs index 28ca9e062a..6d467598c4 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/Window.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/Window.cs @@ -8,6 +8,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Text; +using System.Threading; using System.Threading.Tasks; using Wox.Plugin.Common.VirtualDesktop.Helper; using Wox.Plugin.Common.Win32; @@ -234,12 +235,21 @@ namespace Microsoft.Plugin.WindowWalker.Components NativeMethods.FlashWindow(Hwnd, true); } + /// + /// Helper function to close the window + /// + internal void CloseThisWindowHelper() + { + _ = NativeMethods.SendMessageTimeout(Hwnd, Win32Constants.WM_SYSCOMMAND, Win32Constants.SC_CLOSE, 0, 0x0000, 5000, out _); + } + /// /// Closes the window /// internal void CloseThisWindow() { - _ = NativeMethods.SendMessage(Hwnd, Win32Constants.WM_SYSCOMMAND, Win32Constants.SC_CLOSE); + Thread thread = new(new ThreadStart(CloseThisWindowHelper)); + thread.Start(); } /// diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/WindowProcess.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/WindowProcess.cs index b7bf00e962..ade997f93b 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/WindowProcess.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/WindowProcess.cs @@ -33,6 +33,30 @@ namespace Microsoft.Plugin.WindowWalker.Components get; private set; } + /// + /// Gets a value indicating whether the process is responding or not + /// + internal bool IsResponding + { + get + { + try + { + return Process.GetProcessById((int)ProcessID).Responding; + } + catch (InvalidOperationException) + { + // Thrown when process not exist. + return true; + } + catch (NotSupportedException) + { + // Thrown when process is not running locally. + return true; + } + } + } + /// /// Gets the id of the thread /// diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Properties/Resources.Designer.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Properties/Resources.Designer.cs index 476f64fc8a..a5c8f20f5e 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Properties/Resources.Designer.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Properties/Resources.Designer.cs @@ -141,6 +141,15 @@ namespace Microsoft.Plugin.WindowWalker.Properties { } } + /// + /// Looks up a localized string similar to Not Responding. + /// + public static string wox_plugin_windowwalker_NotResponding { + get { + return ResourceManager.GetString("wox_plugin_windowwalker_NotResponding", resourceCulture); + } + } + /// /// Looks up a localized string similar to No.. /// diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Properties/Resources.resx b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Properties/Resources.resx index f2766272e0..90c63aff6d 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Properties/Resources.resx +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Properties/Resources.resx @@ -202,4 +202,7 @@ This information is only shown in subtitle and tool tip, if you have at least two desktops. + + Not Responding + \ No newline at end of file diff --git a/src/modules/launcher/Wox.Plugin/Common/Win32/NativeMethods.cs b/src/modules/launcher/Wox.Plugin/Common/Win32/NativeMethods.cs index 140a6c37b8..0c2c76e8f0 100644 --- a/src/modules/launcher/Wox.Plugin/Common/Win32/NativeMethods.cs +++ b/src/modules/launcher/Wox.Plugin/Common/Win32/NativeMethods.cs @@ -81,6 +81,9 @@ namespace Wox.Plugin.Common.Win32 [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int msg, int wParam); + [DllImport("user32.dll")] + public static extern int SendMessageTimeout(IntPtr hWnd, uint msg, UIntPtr wParam, IntPtr lParam, int fuFlags, int uTimeout, out int lpdwResult); + [DllImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool CloseHandle(IntPtr hObject);