Search code examples
c#.netwindows-10toastnotifyicon

.NET NotifyIcon displays a toast notification twice in Windows 10


I use the .NET Framework System.Windows.Forms.NotifyIcon class to display a tray icon and tray notifications. When I call ShowBalloonTip on its instance, first the previously displayed notification displays again, then it hides, and only after a while does the expected one is displayed. It seems that historical notifications are displayed first unless I remove them manually from the notification center.

What can I do programmatically to prevent showing the historical notifications again?

Here's how I initialize the notify icon:

notifyIcon.Icon = Properties.Resources.tray_icon;
notifyIcon.Visible = true;

notifyIcon.BalloonTipTitle = Language.TrayMessageTitle;

Here's how I show the toast:

notifyIcon.BalloonTipIcon = icon;
notifyIcon.BalloonTipText = message;
notifyIcon.ShowBalloonTip(0);
  • The icon is either ToolTipIcon.Error or ToolTipIcon.Info, and I don't show a toast with the same icon twice, so they always toggle. But this does not seem to matter.
  • The timeout param is 0, because it's not used since Windows Vista.
  • The toast is displayed as a result of an application state change, not as a result of a user action like clicking.

Solution

  • So I decided to stick with disposing the notify icon instance and recreating it every time I display a toast notification. I don't think it's fine, but I couldn't find a better solution.

    I also dispose the notify icon on application shutdown to clean the notification center from previously displayed notifications. Otherwise, the last one displays first when another application displays a toast.