7

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

JMK
  • 27,273
  • 52
  • 163
  • 280
  • 4
    It's expected behaviour - it affects *every* program that puts an icon in the system tray, not just yours. And there's no API to interact with the notification area. – Damien_The_Unbeliever Jun 11 '12 at 12:27
  • I feared as much, I just had one of those "I know, one of the genius' on stack overflow will have figured this one out" moments – JMK Jun 11 '12 at 12:41
  • Found a blog post which answered my question - http://tinyurl.com/notificationarea – JMK Jun 11 '12 at 13:35
  • 4
    Why are you closing your app with Task Manager? Bets are off when you terminate apps, no cleanup happens. – Hans Passant Jun 11 '12 at 13:54
  • 1
    I'm not closing the app with the task manager, nor am I recommending others to do so. However, people do this regardless and it leaves these icons behind. And yes, this doesn't help me in regards to closing the application, but it works quite well on opening. – JMK Jun 11 '12 at 14:25
  • @JMK Your tinyurl is stale. – Scott Solmer Jul 02 '14 at 19:50
  • @Okuma.Scott So it is, the original url is http://safari-tech.serveblog.net/?p=101 but that site doesn't seem to be online anymore, and I couldn't find the blog post on wayback, but if you look hard enough you may find it :) – JMK Jul 02 '14 at 21:04
  • @Okuma.Scott I think I ended up running some code on startup to clean up the notification area, not perfect but meant there wasn't a bunch of icons littering the place at least – JMK Jul 02 '14 at 21:06
  • @Okuma.Scott I recall this being helpful also - http://stackoverflow.com/q/12973191/969613 – JMK Jul 02 '14 at 21:11

1 Answers1

3

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();
}
Cubi73
  • 1,891
  • 3
  • 31
  • 52