There are many questions on SO asking same doubt. Solution for this is to set
notifyIcon.icon = null
and calling Dispose
for it in FormClosing event.
In my application, there is no such form but has Notification icon which updates on Events.
On creation, I hide my form and make ShowInTaskbar
property false
. Hence I can not have a "FormClosing" or "FormClosed" events.
If this application gets event to exit, It calls Process.GetCurrentProcess().Kill();
to exit.
I have added notifyIcon.icon = null
as well as Dispose before killing, but still icon remains taskbar until I hover mouse over it.
EDIT: If I assume that this behaviour is due to calling GetCurrentProcess().Kill()
, Is there any elegant way to exit from application which will clear all resources and remove icon from system tray.
i can tell you can solve the problem simply using the .dispose() method, but that is not called if you kill the process instead of exit the application.
please refer to Application.Exit if you have built a simple Windows Form application else refer to Environment.Exit that is more general.
I tried all of these and none of them worked for me. After thinking about it for a while I realized that the application creating the "balloon" was exiting before it had a chance to actually dispose of the balloon. I added a while loop just before
Application.Exit()
containing anApplication.DoEvents()
command. This allowed myNotifyIcon1_BalloonTipClosed
to actually finish disposing of the icon before exiting.And the tip closed method: (You need to include the
thisIcon.visible = false
in order for this to work)I don't think WPF has it's own NotifyIcon, does it? If you're using the 3rd party Harcodet.Wpf.TaskbarNotification, then try this:
In order to prevent my app from closing when the window is closed (run in background), I separated the logic for closing the window (hitting the x button in the upper right) and actually shutting it down (through the context menu). To make this work, make your context menu set
_isExplicitClose
to true. Otherwise, it'll just hide the window and continue to run.What this does is, on explicit close, hide tray icon and the form before closing. This way the icon isn't hanging around after the application is shutdown.
Try
Application.DoEvents();
after settingnotifyIcon.Icon
tonull
and disposing:And consider
Environment.Exit(0);
instead ofProcess.GetCurrentProcess().Kill()
.The only solution that worked for me was to use the Closed event and hide and dispose of the icon.
This is normal behaviour, unfortunately; it's due to the way Windows works. You can'r really do anything about it.
See Issue with NotifyIcon not dissappearing on Winforms App for some suggestions, but none of them ever worked for me.
Also see Notify Icon stays in System Tray on Application Close
Microsoft have marked this as "won't fix" on Microsoft Connect.