From 02dd78ff865f07440684c0416c0c64d10a9ed132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20M=C3=B6ller?= Date: Fri, 22 May 2020 18:59:31 +0200 Subject: [PATCH] Changed the notify icon implementation to first try to modify an (existing) icon before trying to add a new one. When the WM_TASKBARCREATED event is raised, sometimes (changing the display scale for example) the icons still exist and fail to get added, while in other cases (explorer task restarted), the icons are lost and need to be added again. This fixes the 8s blocking of the UI thread when changing the display scale. --- GUI/NotifyIconAdv.cs | 49 ++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/GUI/NotifyIconAdv.cs b/GUI/NotifyIconAdv.cs index 15a9a8c..b9de2a2 100644 --- a/GUI/NotifyIconAdv.cs +++ b/GUI/NotifyIconAdv.cs @@ -460,8 +460,7 @@ namespace OpenHardwareMonitor.GUI { data.Info = tipText; data.InfoFlags = (int)tipIcon; - NativeMethods.Shell_NotifyIcon( - NativeMethods.NotifyIconMessage.Modify, data); + NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Modify, data); } } @@ -524,26 +523,28 @@ namespace OpenHardwareMonitor.GUI { if (showNotifyIcon && icon != null) { if (!created) { - int i = 0; - do { - created = NativeMethods.Shell_NotifyIcon( - NativeMethods.NotifyIconMessage.Add, data); - if (!created) { - System.Threading.Thread.Sleep(200); - i++; - } - } while (!created && i < 40); - } else { - NativeMethods.Shell_NotifyIcon( - NativeMethods.NotifyIconMessage.Modify, data); + // try to modify the icon in case it still exists (after WM_TASKBARCREATED) + if (NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Modify, data)) { + created = true; + } else { // modification failed, try to add a new icon + int i = 0; + do { + created = NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Add, data); + if (!created) { + System.Threading.Thread.Sleep(200); + i++; + } + } while (!created && i < 40); + } + } else { // the icon is created already, just modify it + NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Modify, data); } } else { if (created) { int i = 0; - bool deleted = false; + bool deleted; do { - deleted = NativeMethods.Shell_NotifyIcon( - NativeMethods.NotifyIconMessage.Delete, data); + deleted = NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Delete, data); if (!deleted) { System.Threading.Thread.Sleep(200); i++; @@ -667,7 +668,7 @@ namespace OpenHardwareMonitor.GUI { } } - if (message.Msg == NotifyIconWindowsImplementation.WM_TASKBARCREATED) { + if (message.Msg == WM_TASKBARCREATED) { lock (syncObj) { created = false; } @@ -737,6 +738,12 @@ namespace OpenHardwareMonitor.GUI { private static int WM_TASKBARCREATED = NativeMethods.RegisterWindowMessage("TaskbarCreated"); + private enum NotifyIconMessage : int { + Add = 0x0, + Modify = 0x1, + Delete = 0x2 + } + private static class NativeMethods { [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr PostMessage(HandleRef hwnd, int msg, @@ -774,12 +781,6 @@ namespace OpenHardwareMonitor.GUI { public int InfoFlags; } - public enum NotifyIconMessage : int { - Add = 0x0, - Modify = 0x1, - Delete = 0x2 - } - [DllImport("shell32.dll", CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool Shell_NotifyIcon(NotifyIconMessage message,