3

I create my tray icon with System.Windows.Forms.NotifyIcon

However, after the application closes. The icon continues to linger until I manually mouse over it.

How can I prevent it from lingering or clear all the old ones when a new instance is run?

John
  • 5,942
  • 3
  • 42
  • 79
  • This happens if you don't dispose your TrayIcon when the application gets closed. Another case is when your application crashes. But in that case it happens also to all other applications using a tray icon regardless if they are written in C#, C++ or any other language that can produce windows applications. – Oliver Oct 16 '17 at 06:18

1 Answers1

8

I did not reproduce your problem and I'm running on Windows 10 Creators Update.

But I found that you could Dispose your NotifyIcon when you close your application. Dispose can remove your icon out of the tray area of the taskbar.

I guess that you may have exited your program unexpectedly and that will cause your problem. You should check whether System.Exit() or other unsafe exit method is called.

You can see the code below to know how to call the Dispose:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);
        // Call Dispose to remove the icon out of notification area of Taskbar.
        notifyIcon1.Dispose();
    }
}
walterlv
  • 2,366
  • 13
  • 73
  • 1
    When debugging, the application terminates without calling any callbacks when I click the stop button. That is when they build up. Is there any way to stop it then? – John Oct 16 '17 at 20:00
  • Windows don't handle notifyicon shown or hidden, even the whole process exited. The Windows API Shell_NotifyIcon(DWORD dwMessage, PNOTIFYICONDATA lpdata) is the only way to control it. – walterlv Oct 16 '17 at 23:38