Issue with NotifyIcon not disappearing on Winforms App

前端 未结 7 1799
暗喜
暗喜 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 17:45

    This is what I'm doing in WPF.

    I am using this in conjunction to David Anson's Minimize to tray sample app, which lets you hook up a tray icon to a window (you may have multiple windows open).

    Just added this code to the constructor for MinimizeToTrayInstance.

    _window.Closed += (s, e) => 
    {
            if (_notifyIcon != null)
            {
                _notifyIcon.Visible = false;
                _notifyIcon.Dispose();
                _notifyIcon = null;
            }
    };
    
    0 讨论(0)
  • 2020-12-16 17:48

    before im sorry for my bad english. if u use "end" for exit program. then dont close notify icon. before u will close notifyicon later close form. u need to use me.close() for run form closing

    example its work...

    notifyIcon1.Icon = Nothing
    notifyIcon1.Visible = False
    notifyIcon1.Dispose()
    Me.Close()
    

    but its not work

    End
    

    or only

    Me.Close()
    
    0 讨论(0)
  • 2020-12-16 17:49

    Have you overridden the dispose method of the object where you've initialised the notifyIcon to also dispose the notifyIcon?

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            notifyIcon.Dispose();
            notifyIcon = null;
        }
        base.Dispose(disposing);
    }
    
    0 讨论(0)
  • This code works for me, but I don't know how you are keeping your application alive, so... without further ado:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    static class Program
    {
        static System.Threading.Timer test = 
            new System.Threading.Timer(Ticked, null, 5000, 0);
    
        [STAThread]
        static void Main(string[] args)
        {
            NotifyIcon ni = new NotifyIcon();
            ni.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
            ni.Visible = true;
    
            Application.Run();
            ni.Visible = false;
        }
    
        static void Ticked(object o) {
            Application.Exit();
        }
    }
    
    0 讨论(0)
  • 2020-12-16 17:57

    This code worked for me

    this.Closed += (a, b) =>
                {
                    if (notifyIcon1 != null)
                    {
                        notifyIcon1.Dispose();
                        notifyIcon1.Icon = null;
                        notifyIcon1.Visible = false;
                    }
                };
    
    0 讨论(0)
  • 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
                }
            }
    
    0 讨论(0)
提交回复
热议问题