12

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

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

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

应用程序会访问括号内的代码,但在尝试设置 Visible = false 时会引发 null ref 异常。

我已经在几个地方阅读了将其放入表单关闭事件的内容,但该代码永远不会被击中(也许是因为我没有显示这样的表单?)。

我可以把这段代码放在哪里让它真正起作用?如果我不把它放进去,我会在托盘中看到令人讨厌的挥之不去的图标,直到你将鼠标移到它上面。

干杯。

编辑

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

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

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

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

这有帮助吗?

4

7 回答 7

12

在 Windows 7 上,我还必须将 Icon 属性设置为 null。否则,该图标会在应用程序关闭后保留在托盘的“隐藏图标”弹出窗口中。有人。

// 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
            }
        }
于 2010-11-21T16:16:49.520 回答
4

这段代码对我有用,但我不知道你是如何让你的应用程序保持活力的,所以......事不宜迟:

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();
    }
}
于 2009-07-01T08:08:29.290 回答
3

这就是我在 WPF 中所做的。

我将它与 David Anson 的Minimize to tray sample app结合使用,它可以让您将托盘图标连接到一个窗口(您可能打开了多个窗口)。

刚刚将此代码添加到MinimizeToTrayInstance.

_window.Closed += (s, e) => 
{
        if (_notifyIcon != null)
        {
            _notifyIcon.Visible = false;
            _notifyIcon.Dispose();
            _notifyIcon = null;
        }
};
于 2010-10-01T00:41:21.037 回答
0

有时 Application_Exit 事件可以多次引发只需将 notifyIcon = null; 到底

if (notifyIcon != null)
{
    notifyIcon.Visible = false;
    notifyIcon.Dispose();
    notifyIcon = null;
}
于 2009-07-01T08:21:21.090 回答
0

这段代码对我有用

this.Closed += (a, b) =>
            {
                if (notifyIcon1 != null)
                {
                    notifyIcon1.Dispose();
                    notifyIcon1.Icon = null;
                    notifyIcon1.Visible = false;
                }
            };
于 2012-03-18T15:34:40.970 回答
0

您是否重写了已初始化 notifyIcon 的对象的 dispose 方法以也处置 notifyIcon?

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        notifyIcon.Dispose();
        notifyIcon = null;
    }
    base.Dispose(disposing);
}
于 2011-11-03T12:09:41.063 回答
0

在我为我糟糕的英语感到抱歉之前。如果你使用“结束”退出程序。然后不要关闭通知图标。在您关闭通知图标之前,请稍后关闭表单。你需要使用 me.close() 来关闭运行表单

例如它的工作...

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

但它不起作用

End

或仅

Me.Close()
于 2020-05-19T04:19:55.583 回答