Issue with NotifyIcon not disappearing on Winforms App

泪湿孤枕 提交于 2019-12-18 04:03:28

问题


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 of it in the Application_Exit event, as follows:

        if (notifyIcon != null)
        {
            notifyIcon.Visible = false;
            notifyIcon.Dispose();
        }

The app gets to the code inside the brackets, but throws a null ref exception when it tries to set Visible = false.

I've read in a few places to put it in the form closing event, but that code never gets hit (maybe as I don't have a form showing as such?).

Where can I put this code so it actually works? If I don't put it in, I get the annoying lingering icon in the tray until you move the mouse over it.

Cheers.

EDIT

Just something extra I've noticed...........

I'm using ClickOnce in the app.........if I just exit the app via the ContextMenu on the NotifyIcon, no exception is logged.

Just when the Application_Exit event is fired after the applicaiton has checked for an upgrade here..

private void CheckForUpdate()
{
    EventLogger.Instance.LogEvent("Checking for Update");
    if (ApplicationDeployment.IsNetworkDeployed && ApplicationDeployment.CurrentDeployment.CheckForUpdate())
    {
        EventLogger.Instance.LogEvent("Update available - updating");
        ApplicationDeployment.CurrentDeployment.Update();
        Application.Restart();
    }
}

Does this help?


回答1:


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
            }
        }



回答2:


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();
    }
}



回答3:


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;
        }
};



回答4:


Sometimes Application_Exit event can be raised several times Just put notifyIcon = null; in the end

if (notifyIcon != null)
{
    notifyIcon.Visible = false;
    notifyIcon.Dispose();
    notifyIcon = null;
}



回答5:


This code worked for me

this.Closed += (a, b) =>
            {
                if (notifyIcon1 != null)
                {
                    notifyIcon1.Dispose();
                    notifyIcon1.Icon = null;
                    notifyIcon1.Visible = false;
                }
            };



回答6:


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);
}


来源:https://stackoverflow.com/questions/1067844/issue-with-notifyicon-not-disappearing-on-winforms-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!