本文介绍了问题与NotifyIcon的不dissappearing的WinForms应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.net 3.5 C#的WinForms应用程序。它有没有GUI是这样,只是一个带有文本菜单的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的设置为可见=假,并在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();
        }

该应用程序获取到括号中的代码,但是会引发空裁判例外当它试图设置可见=假。

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.

干杯。

修改

EDIT

只是额外的东西我已经注意到了..... ......

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

我使用ClickOnce的应用程序.........如果我只是通过文本菜单退出的NotifyIcon的应用没有记录异常。

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



这是否帮助?

Does this help?

推荐答案

在Windows 7中,我必须还设置了图标属性设置为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的不dissappearing的WinForms应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 10:54