Notify Icon stays in System Tray on Application Close
Asked Answered
B

1

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

Bedcover answered 11/6, 2012 at 12:22 Comment(9)
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.Langue
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" momentsBedcover
Found a blog post which answered my question - tinyurl.com/notificationareaBedcover
Why are you closing your app with Task Manager? Bets are off when you terminate apps, no cleanup happens.Sennacherib
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.Bedcover
@Bedcover Your tinyurl is stale.Sedberry
@Okuma.Scott So it is, the original url is 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 :)Bedcover
@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 leastBedcover
@Okuma.Scott I recall this being helpful also - https://mcmap.net/q/246963/-c-notifyicon-stays-in-tray-after-the-balloon-tip-has-been-closed/969613Bedcover
W
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();
}
Williwaw answered 14/2, 2015 at 0:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.