From 96efc06ba93143050899add57556037d65b44573 Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Mon, 2 Mar 2026 18:01:06 -0500 Subject: [PATCH] Send identify message with a timeout --- Library/WindowManager.cs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Library/WindowManager.cs b/Library/WindowManager.cs index 8199c44..d807679 100644 --- a/Library/WindowManager.cs +++ b/Library/WindowManager.cs @@ -15,6 +15,16 @@ public static partial class WindowManager Allow = 1 } + [Flags] + private enum SendMessageTimeoutFlags : uint + { + Normal = 0x0, + Block = 0x1, + AbortIfHung = 0x2, + NoTimeoutIfNotHung = 0x8, + ErrorOnExit = 0x20 + } + [LibraryImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static partial void EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam); @@ -31,6 +41,17 @@ public static partial class WindowManager [LibraryImport("user32.dll", EntryPoint = "SendMessageW", SetLastError = true)] private static partial IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); + [LibraryImport("user32.dll", EntryPoint = "SendMessageTimeoutW", SetLastError = true)] + private static partial IntPtr SendMessageTimeout( + IntPtr windowHandle, + int msg, + IntPtr wParam, + IntPtr lParam, + SendMessageTimeoutFlags flags, + uint timeout, + out IntPtr result); + + [LibraryImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static partial void ChangeWindowMessageFilterEx(IntPtr hWnd, int msg, ChangeWindowMessageFilterExAction action, IntPtr changeInfo); @@ -101,7 +122,7 @@ public static partial class WindowManager if (hWnd == _excludeHandle) return true; - var identifyResult = SendMessage(hWnd, IdentifyMessage, IntPtr.Zero, IntPtr.Zero); + SendMessageTimeout(hWnd, IdentifyMessage, IntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.Normal, 100, out var identifyResult); if (identifyResult == IdentifyMessage) {