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

后端 未结 14 1357
清歌不尽
清歌不尽 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: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();
    }
    

提交回复
热议问题