NotifyIcon 在 Winforms 应用程序上没有消失的问题 [英] Issue with NotifyIcon not disappearing on Winforms App

查看:32
本文介绍了NotifyIcon 在 Winforms 应用程序上没有消失的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .Net 3.5 C# Winforms 应用程序.它没有 GUI,只有一个带有 ContextMenu 的 NotifyIcon.

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

我尝试将 NotifyIcon 设置为 visible=false 并在 Application_Exit 事件中处理它,如下所示:

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

应用程序获取括号内的代码,但在尝试设置 Visible = false 时抛出空引用异常.

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.

干杯.

编辑

我注意到了一些额外的东西.......

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

我在应用程序中使用 ClickOnce..........如果我只是通过 NotifyIcon 上的 ContextMenu 退出应用程序,则不会记录任何异常.

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

就在应用程序在此处检查升级后触发 Application_Exit 事件时..

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

这有帮助吗?

推荐答案

在 Windows 7 上,我还必须将 Icon 属性设置为 null.否则,应用程序关闭后,图标仍保留在托盘的隐藏图标"弹出窗口中.HTH某人.

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

这篇关于NotifyIcon 在 Winforms 应用程序上没有消失的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆