Hi, I have a window service for my application. When i stops that by killing process with task manager, tray icon does not disappears. Is it a window bug or something else? Do we have a solution for that problem? Thanks in advance :).
Move your mouse over the icon, and it will disappear. I've noticed this behavior in all versions of windows, including Win 7.
There is no solution to this problem. If you kill process with task manager, it does not receive termination notification, and hence can not remove its icon from the tray. Try avoiding killing process this way. You can use net start/stop
to kill a service or services.msc
GUI.
I often notice that too, with various applications. The death of the application is only noticed when you move the mouse over the icon.
I think the "bug" is with Windows, not your application. (I'm reluctant to call it a "bug", per se, because it was probably a conscious decision to leave this in. Explorer could check whether applications that registered icons are still running, but that might be too expensive.)
You can let the icon disappear by calling the Dispose()-method of the specified NotifyIcon-object. In most cases these Container-object isn't part of the tree of components in your application so it will not disappear by killing the proces. When the user moves over the icon, the icon doesn't find it parent so it dissapears. But by calling the Dispose-method, it disapeared at least in my applications. So:
//creating a NotifyIcon
NotifyIcon notifyicon = new NotifyIcon();
notifyicon.Text = "Text";
notifyicon.Visible = true;
notifyicon.Icon = new Icon(GetType(),"Icon.ico");
//let it disappear
notifyicon.Dispose();
If an application is forcefully terminated (e.g. through Task Manager), then Windows does not remove the notification icon. Windows Explorer doesn't even notice that the application has gone away until it attempts to send a message (usually a mouse movement message) to the window that owns the notification icon. At that point, Windows will remove the now dead icon from the notification area.
Given that you can't intercept TerminateProcess, there's nothing that your program can do about this by itself.
I guess that Windows Explorer could watch for the owner window being destroyed (as when the application quits unexpectedly), but it doesn't.
Even if the application is shut down gracefully, it must still remember to remove any of its notification icons. That is: if you don't call Shell_NotifyIcon(NIM_DELETE)
(the equivalent of NotifyIcon.Dispose
) when your application shuts down (even gracefully), the icon will remain there until the mouse moves over it.
Oh, and if this is a service process that's displaying the notification icon, be aware that session 0 isolation in Windows Vista and Windows 7 will break it.
I've done it by handling the ThreadException event and disposing the tray icon in that event handler.
I think I found the cause and a workaround. I posted the answer on my blog below, and have included a sample WinForm project to demonstrate it. I hope this solves your issues as well.