Skip to content

Commit 02dd78f

Browse files
committed
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.
1 parent 9abc890 commit 02dd78f

1 file changed

Lines changed: 25 additions & 24 deletions

File tree

GUI/NotifyIconAdv.cs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,7 @@ public void ShowBalloonTip(int timeout, string tipTitle, string tipText,
460460
data.Info = tipText;
461461
data.InfoFlags = (int)tipIcon;
462462

463-
NativeMethods.Shell_NotifyIcon(
464-
NativeMethods.NotifyIconMessage.Modify, data);
463+
NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Modify, data);
465464
}
466465
}
467466

@@ -524,26 +523,28 @@ private void UpdateNotifyIcon(bool showNotifyIcon) {
524523

525524
if (showNotifyIcon && icon != null) {
526525
if (!created) {
527-
int i = 0;
528-
do {
529-
created = NativeMethods.Shell_NotifyIcon(
530-
NativeMethods.NotifyIconMessage.Add, data);
531-
if (!created) {
532-
System.Threading.Thread.Sleep(200);
533-
i++;
534-
}
535-
} while (!created && i < 40);
536-
} else {
537-
NativeMethods.Shell_NotifyIcon(
538-
NativeMethods.NotifyIconMessage.Modify, data);
526+
// try to modify the icon in case it still exists (after WM_TASKBARCREATED)
527+
if (NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Modify, data)) {
528+
created = true;
529+
} else { // modification failed, try to add a new icon
530+
int i = 0;
531+
do {
532+
created = NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Add, data);
533+
if (!created) {
534+
System.Threading.Thread.Sleep(200);
535+
i++;
536+
}
537+
} while (!created && i < 40);
538+
}
539+
} else { // the icon is created already, just modify it
540+
NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Modify, data);
539541
}
540542
} else {
541543
if (created) {
542544
int i = 0;
543-
bool deleted = false;
545+
bool deleted;
544546
do {
545-
deleted = NativeMethods.Shell_NotifyIcon(
546-
NativeMethods.NotifyIconMessage.Delete, data);
547+
deleted = NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Delete, data);
547548
if (!deleted) {
548549
System.Threading.Thread.Sleep(200);
549550
i++;
@@ -667,7 +668,7 @@ private void WndProc(ref Message message) {
667668
}
668669
}
669670

670-
if (message.Msg == NotifyIconWindowsImplementation.WM_TASKBARCREATED) {
671+
if (message.Msg == WM_TASKBARCREATED) {
671672
lock (syncObj) {
672673
created = false;
673674
}
@@ -737,6 +738,12 @@ protected override void WndProc(ref Message m) {
737738
private static int WM_TASKBARCREATED =
738739
NativeMethods.RegisterWindowMessage("TaskbarCreated");
739740

741+
private enum NotifyIconMessage : int {
742+
Add = 0x0,
743+
Modify = 0x1,
744+
Delete = 0x2
745+
}
746+
740747
private static class NativeMethods {
741748
[DllImport("user32.dll", CharSet = CharSet.Auto)]
742749
public static extern IntPtr PostMessage(HandleRef hwnd, int msg,
@@ -774,12 +781,6 @@ public class NotifyIconData {
774781
public int InfoFlags;
775782
}
776783

777-
public enum NotifyIconMessage : int {
778-
Add = 0x0,
779-
Modify = 0x1,
780-
Delete = 0x2
781-
}
782-
783784
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
784785
[return: MarshalAs(UnmanagedType.Bool)]
785786
public static extern bool Shell_NotifyIcon(NotifyIconMessage message,

0 commit comments

Comments
 (0)