即使应用程序关闭,NotifyIcon仍保留在托盘中,但在鼠标悬停时消失

时间:2013-02-06 07:36:07

标签: c# notifyicon

关于SO的问题有很多疑问。 解决方法是设置

notifyIcon.icon = null并在FormClosing事件中调用Dispose

在我的应用程序中,没有这样的表单,但具有更新事件的通知图标。 在创建时,我会隐藏我的表单并生成ShowInTaskbar属性false。因此我不能拥有一个" FormClosing"或" FormClosed"事件

如果此应用程序要退出事件,则会调用Process.GetCurrentProcess().Kill();退出。

我在杀戮之前添加了notifyIcon.icon = null以及Dispose,但是在我将鼠标悬停在任意栏上之前,图标仍然是任务栏。

编辑:如果我认为此行为是由于调用GetCurrentProcess().Kill(),是否有任何优雅的方式退出应用程序,这将清除所有资源并从系统托盘中删除图标。< / p>

14 个答案:

答案 0 :(得分:46)

您可以设置

notifyIcon1.Visible = false;

OR

notifyIcon.Icon = null;

在表格结束活动中。

答案 1 :(得分:13)

对我有用的唯一解决方案是使用Closed事件并隐藏和处理图标。

icon.BalloonTipClosed += (sender, e) => { 
                                            var thisIcon = (NotifyIcon)sender;
                                            thisIcon.Visible = false;
                                            thisIcon.Dispose(); 
                                        };

答案 2 :(得分:8)

notifyIcon.Visible = False事件

中使用FormClosing

答案 3 :(得分:5)

我试过这段代码。我觉得这更容易。我会为几个工具或事件编写它。我希望它对你有所帮助。

按“退出”或“关闭”按钮时,如果要执行此操作,请使用此代码:

    private void ExitButton_Click(object sender, EventArgs e)
    {
            notifyIcon.Dispose;
            Application.Exit();        //   or   this.Close();
    }

如果要在表单关闭时执行此操作,请使用此代码:

    private void Form1_FormClosing(object sender, EventArgs e)
    {
            notifyIcon.Dispose;
            Application.Exit();        //   or   this.Close();
    }

重要的代码是:

    notifyIcon.Dispose;

答案 4 :(得分:5)

组件必须按照正确的顺序进行处理:

NotifyIcon.Icon.Dispose();

NotifyIcon.Dispose();

将此添加到MainWindow结束事件。

希望这会有所帮助。

答案 5 :(得分:2)

不幸的是,这是正常行为;这是由于Windows的工作方式。你真的可以做任何事情。

请参阅Issue with NotifyIcon not dissappearing on Winforms App以获取一些建议,但这些建议都没有为我工作过。

另见Notify Icon stays in System Tray on Application Close

Microsoft已在Microsoft Connect上将此标记为“无法修复”。

答案 6 :(得分:2)

我不认为WPF拥有它自己的NotifyIcon,是吗?如果您正在使用第三方Harcodet.Wpf.TaskbarNotification,请尝试以下操作:

为了防止我的应用程序在窗口关闭时关闭(在后台运行),我将关闭窗口的逻辑分开(点击右上角的x按钮)并实际关闭它(通过上下文菜单) )。要使其工作,请将上下文菜单设置为_isExplicitClose为true。否则,它只是隐藏窗口并继续运行。

这样做,在显式关闭时,隐藏托盘图标和关闭前的表单。这样,在关闭应用程序后,图标就不会挂起。

private bool _isExplicitClose;
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
    base.OnClosing(e);

    if (!_isExplicitClose)
    {
        e.Cancel = true;
        Hide();
    }
}

protected void QuitService(object sender, RoutedEventArgs e)
{
   _isExplicitClose = true;
   TaskbarIcon.Visibility = Visibility.Hidden;
   Close();
}

答案 7 :(得分:1)

我可以告诉你可以简单地使用.dispose()方法解决问题,但是如果你终止进程而不是退出应用程序就不会调用它。

如果您构建了一个简单的Windows窗体应用程序,请参阅Application.Exit,否则请参阅更为通用的Environment.Exit

答案 8 :(得分:1)

Application.DoEvents();设置为notifyIcon.Icon并处置后,请尝试null

notifyIcon.Icon = null;
notifyIcon.Dispose();
Application.DoEvents();

并考虑Environment.Exit(0);而不是Process.GetCurrentProcess().Kill()

答案 9 :(得分:1)

我尝试了所有这些方法,但没有一个对我有用。考虑了一会儿之后,我意识到创建“气球”的应用程序在有机会真正处置气球之前就退出了。我在包含Application.Exit()命令的Application.DoEvents()之前添加了一个while循环。这使我的NotifyIcon1_BalloonTipClosed在退出之前实际上已经完成了图标的处理。

while (notifyIcon1.Visible)
{
    Application.DoEvents();
}
Application.Exit();

技巧关闭方法:(您需要添加thisIcon.visible = false才能使它起作用)

private void NotifyIcon1_BalloonTipClosed(object sender, EventArgs e)
{
    var thisIcon = (NotifyIcon)sender;
    thisIcon.Icon = null;
    thisIcon.Visible = false;
    thisIcon.Dispose();
}

答案 10 :(得分:0)

我和你有完全相同的问题。

正确的方法是向进程发送WM_CLOSE消息 我使用了本文中的c#代码 http://social.msdn.microsoft.com/Forums/vstudio/en-US/82992842-80eb-43c8-a9e6-0a6a1d19b00f/terminating-a-process-in-a-friendly-way

答案 11 :(得分:0)

已经给出了正确答案。但是你必须提供延迟,例如使用计时器。只有这样,应用程序仍然可以在后台删除图标。

private System.Windows.Forms.Timer mCloseAppTimer;
private void ExitButton_Click(object sender, EventArgs e) 
{ 
    notifyIcon.Visible = false; notifyIcon.Dispose; 
    mCloseAppTimer = new System.Windows.Forms.Timer(); 
    mCloseAppTimer.Interval = 100; 
    mCloseAppTimer.Tick += new EventHandler(OnCloseAppTimerTick); 
} 
private void OnCloseAppTimerTick(object sender, EventArgs e) 
{ 
    Environment.Exit(0); // other exit codes are also possible 
}

答案 12 :(得分:0)

编辑...... Designer.cs的代码如下编码。

        protected override void Dispose(bool disposing)
           {
           if (disposing )
               {
               this.notifyicon.Dispose();
               }
           base.Dispose(disposing);
           }

答案 13 :(得分:0)

对我有用的唯一方法是:

  1. 在设计屏幕上,更改notifyicon1属性visible = false

  2. 在主窗体“已激活”事件上插入以下代码:

NotifyIcon1.Visible = True
  1. 在主表单“关闭”事件上插入以下代码:
NotifyIcon1.Visible = false
NotifyIcon1.Icon.Dispose()
NotifyIcon1.Dispose()