NotifyIcon remains in Tray even after application

2019-01-22 10:35发布

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.

标签: c# notifyicon
13条回答
The star\"
2楼-- · 2019-01-22 11:22

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.

查看更多
叛逆
3楼-- · 2019-01-22 11:22

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 an Application.DoEvents() command. This allowed my NotifyIcon1_BalloonTipClosed to actually finish disposing of the icon before exiting.

while (notifyIcon1.Visible)
{
    Application.DoEvents();
}
Application.Exit();

And the tip closed method: (You need to include the thisIcon.visible = false in order for this to work)

private void NotifyIcon1_BalloonTipClosed(object sender, EventArgs e)
{
    var thisIcon = (NotifyIcon)sender;
    thisIcon.Icon = null;
    thisIcon.Visible = false;
    thisIcon.Dispose();
}
查看更多
何必那么认真
4楼-- · 2019-01-22 11:23

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.

private bool _isExplicitClose;
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
    base.OnClosing(e);

    if (!_isExplicitClose)
    {
        e.Cancel = true;
        Hide();
    }
}

protected void QuitService(object sender, RoutedEventArgs e)
{
   _isExplicitClose = true;
   TaskbarIcon.Visibility = Visibility.Hidden;
   Close();
}
查看更多
相关推荐>>
5楼-- · 2019-01-22 11:24

Try Application.DoEvents(); after setting notifyIcon.Icon to null and disposing:

notifyIcon.Icon = null;
notifyIcon.Dispose();
Application.DoEvents();

And consider Environment.Exit(0); instead of Process.GetCurrentProcess().Kill().

查看更多
Juvenile、少年°
6楼-- · 2019-01-22 11:28

The only solution that worked for me was to use the Closed event and hide and dispose of the icon.

icon.BalloonTipClosed += (sender, e) => { 
                                            var thisIcon = (NotifyIcon)sender;
                                            thisIcon.Visible = false;
                                            thisIcon.Dispose(); 
                                        };
查看更多
甜甜的少女心
7楼-- · 2019-01-22 11:29

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.

查看更多
登录 后发表回答