Disappearing System Tray icons

2019-05-23 11:43发布

问题:

Im creating a system tray application in visual studio 2010, using C#.

When the application starts i create my thread and a system tray icon. THe icon shows, however whenever i mouse over the icon, it disappears ( the application is still running ), and even if i click the button to show all hidden icons, it doesnt display.

However, if i dont try to mouse over on it, then it stays their in the system tray.

Any Thoughts or experience?

Thanks in advance


Thanks for the answers guys.

Uhh, something i did to fix before so although for those who are perhaps curious.

I initially wasnt using a windows form, and this is when the problem occured. However when i set my app to be a windows form, and just hide the form, and not show it in the taskbar, it worked.

回答1:

Paste this code into your form class:

    protected override void OnFormClosing(FormClosingEventArgs e) {
        notifyIcon1.Visible = false;
        base.OnFormClosing(e);
    }

This ensures the icon will disappear without lingering in the tray. Now set a breakpoint on that code and find out why your form is closing. Copy and paste the stack trace into your question if you cannot figure out why.



回答2:

This means that tray icon has been removed. That usually happens after process terminates but the tray stays there - it is a windows bug.

So for some reason, your tray icon perhaps "crashes".

Without seeing your code, it would be impossible to comment any further.



回答3:

If you are creating the icon object and letting it go out of scope without any reference to it, the next garbage collection will call it's destructor and this will happens.



回答4:

When the Windows Explorer restarts ,windows will clear all the icons in the notification area and sends a broadcast message TaskbarCreated .One has to use the message to add the notification icon again .

You can use the following code to listen to the event :

UINT WM_TaskBarCreated = ::RegisterWindowMessage(L"TaskbarCreated");

and the use windowproc or MessageHandler to add the icon back in the notifiation area.