NotifyIcon remains in Tray even after application closing but disappears on Mouse Hover

后端 未结 14 1206
清歌不尽
清歌不尽 2020-12-03 00:08

There are many questions on SO asking same doubt. Solution for this is to set

notifyIcon.icon = null and calling Dispose for it in FormClo

相关标签:
14条回答
  • 2020-12-03 01:00

    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(); 
                                            };
    
    0 讨论(0)
  • 2020-12-03 01:01

    You can either set

    notifyIcon1.Visible = false;
    

    OR

    notifyIcon.Icon = null;
    

    in the form closing event.

    0 讨论(0)
  • 2020-12-03 01:03

    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();
    }
    
    0 讨论(0)
  • 2020-12-03 01:04

    Use this code when you want to do it when you press the Exit or Close button:

    private void ExitButton_Click(object sender, EventArgs e)
    {
        notifyIcon.Dispose();
        Application.Exit(); // or this.Close();
    }
    

    Use this code when you want to do it when the form is closing:

    private void Form1_FormClosing(object sender, EventArgs e)
    {
        notifyIcon.Dispose();
        Application.Exit(); // or this.Close();
    }
    

    The important code is this:

    notifyIcon.Dispose();
    
    0 讨论(0)
  • 2020-12-03 01:05

    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.

    0 讨论(0)
  • 2020-12-03 01:06

    Use notifyIcon.Visible = False in FormClosing event

    0 讨论(0)
提交回复
热议问题