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.

This commit is contained in:
Michael Möller 2020-05-22 18:59:31 +02:00
parent 9abc8906e4
commit 02dd78ff86

View File

@ -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,