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.
The right answer has already been given. But you must also provide a delay, for example with a timer. Only then the application can still remove the icon in the background.
You can either set
OR
in the form closing event.
Use
notifyIcon.Visible = False
inFormClosing
eventI had the exact same problem as you.
The proper way are send WM_CLOSE message to a process.
I use the c# code I found in this article.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/82992842-80eb-43c8-a9e6-0a6a1d19b00f/terminating-a-process-in-a-friendly-way
Components just must be disposed in the right order like this :
Add this to the
MainWindow
closing event.Hope this will help.
I tried this code. I thing this is easier. I will write it for several tools or events. I hope it will be helpful for you.
Use this code when you want to do it when you press the Exit or Close button:
Use this code when you want to do it when form is closing:
The important code is this: