Issue with NotifyIcon not disappearing on Winforms App

前端 未结 7 1826
暗喜
暗喜 2020-12-16 17:38

I\'ve got a .Net 3.5 C# Winforms app. It\'s got no GUI as such, just a NotifyIcon with a ContextMenu.

I\'ve tried to set the NotifyIcon to visible=false and dispose

7条回答
  •  爱一瞬间的悲伤
    2020-12-16 18:04

    On Windows 7, I had to also set the Icon property to null. Otherwise, the icon remained in the tray's "hidden icons" popup after the application had closed. HTH somebody.

    // put this inside the window's class constructor
    Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
    
    
            private void OnApplicationExit(object sender, EventArgs e)
            {
    
                try
                {
                    if (trayIcon != null)
                    {
                        trayIcon.Visible = false;
                        trayIcon.Icon = null; // required to make icon disappear
                        trayIcon.Dispose();
                        trayIcon = null;
                    }
    
                }
                catch (Exception ex)
                {
                    // handle the error
                }
            }
    

提交回复
热议问题