Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notify Icon stays in System Tray on Application Close

I have an application which runs only from the System Tray, it's only purpose is to provide the user with information via Ballon Tips.

It's running well, apart from one minor annoyance. When the application is closed using the Task Manager (as opposed to using the context menu) the icon sticks around in the system tray, until you hover over it, then when another instance is opened you get a second icon sitting beside the first.

My Form Closed event looks like this, it does nothing:

private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
{
    ntfyIcon.Visible = false;
    ntfyIcon.Icon = null;
    ntfyIcon.Visible = false;
    ntfyIcon.Dispose();
}

This has been reported on Microsoft Connect and has been closed by Microsoft under Won't Fix as, apparently, this is what is supposed to happen but I was hoping that somebody had a solution.

I was thinking something along the lines of cleaning the system tray on application open?

Thanks

like image 602
JMK Avatar asked Jun 11 '12 12:06

JMK


1 Answers

For me, it works when calling Application.DoEvents() after settings Icon to null and disposing the NotifyIcon.

private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
{
    ntfyIcon.Icon = null;
    ntfyIcon.Dispose();
    System.Windows.Forms.Application.DoEvents();
}
like image 180
Cubi73 Avatar answered Oct 01 '22 12:10

Cubi73