Winforms 应用程序上 NotifyIcon 未消失的问题

发布于 2024-07-26 01:35:07 字数 1148 浏览 5 评论 0原文

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

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

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

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

我在一些地方读过将其放入表单关闭事件中,但该代码永远不会被命中(也许是因为我没有这样显示的表单?)。

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

干杯。

编辑

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

我在应用程序中使用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();
    }
}

这有帮助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

莫相离 2024-08-02 01:35:07

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

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

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
            }
        }
美羊羊 2024-08-02 01:35:07

这段代码对我有用,但我不知道你如何让你的应用程序保持活动状态,所以......不用多说:

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

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();
    }
}
陌生 2024-08-02 01:35:07

这就是我在 WPF 中所做的。

我将其与 David Anson 的 最小化到托盘示例应用,它可以让您将托盘图标连接到窗口(您可能有多个窗户打开)。

刚刚将此代码添加到 MinimizeToTrayInstance 的构造函数中。

_window.Closed += (s, e) => 
{
        if (_notifyIcon != null)
        {
            _notifyIcon.Visible = false;
            _notifyIcon.Dispose();
            _notifyIcon = null;
        }
};

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;
        }
};
¢好甜 2024-08-02 01:35:07

有时 Application_Exit 事件可能会引发多次
只要把notifyIcon = null; 到底

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

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;
}
以歌曲疗慰 2024-08-02 01:35:07

这段代码对我有用

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

This code worked for me

this.Closed += (a, b) =>
            {
                if (notifyIcon1 != null)
                {
                    notifyIcon1.Dispose();
                    notifyIcon1.Icon = null;
                    notifyIcon1.Visible = false;
                }
            };
雨落星ぅ辰 2024-08-02 01:35:07

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

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        notifyIcon.Dispose();
        notifyIcon = null;
    }
    base.Dispose(disposing);
}

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);
}
猫卆 2024-08-02 01:35:07

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

示例
它的工作......

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

但它不是工作

End

或只是

Me.Close()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文