C# NotifyIcon 在 Winforms 应用程序上没有消失的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1067844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Issue with NotifyIcon not disappearing on Winforms App
提问by
I've got a .Net 3.5 C# Winforms app. It's got no GUI as such, just a NotifyIcon with a ContextMenu.
我有一个 .Net 3.5 C# Winforms 应用程序。它没有 GUI,只有一个带有 ContextMenu 的 NotifyIcon。
I've tried to set the NotifyIcon to visible=false and dispose of it in the Application_Exit event, as follows:
我尝试将 NotifyIcon 设置为 visible=false 并在 Application_Exit 事件中处理它,如下所示:
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.
应用程序访问方括号内的代码,但在尝试设置 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.
我在应用程序中使用 ClickOnce ......如果我只是通过 NotifyIcon 上的 ContextMenu 退出应用程序,则不会记录任何异常。
Just when the Application_Exit event is fired after the applicaiton has checked for an upgrade here..
就在应用程序在此处检查升级后触发 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();
}
}
Does this help?
这有帮助吗?
回答by Matthew Scharley
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();
}
}
回答by Bogdan_Ch
Sometimes Application_Exit event can be raised several times Just put notifyIcon = null; in the end
有时 Application_Exit 事件可以多次引发 只需放置 notifyIcon = null; 到底
if (notifyIcon != null)
{
notifyIcon.Visible = false;
notifyIcon.Dispose();
notifyIcon = null;
}
回答by Simon_Weaver
This is what I'm doing in WPF.
这就是我在 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).
我将此与 David Anson 的最小化到托盘示例应用程序结合使用,它允许您将托盘图标连接到一个窗口(您可能打开了多个窗口)。
Just added this code to the constructor for MinimizeToTrayInstance
.
刚刚将此代码添加到MinimizeToTrayInstance
.
_window.Closed += (s, e) =>
{
if (_notifyIcon != null)
{
_notifyIcon.Visible = false;
_notifyIcon.Dispose();
_notifyIcon = null;
}
};
回答by Dave
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.
在 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
}
}
回答by JDandChips
Have you overridden the dispose method of the object where you've initialised the notifyIcon to also dispose the notifyIcon?
您是否重写了已初始化 notifyIcon 的对象的 dispose 方法来处理 notifyIcon ?
protected override void Dispose(bool disposing)
{
if (disposing)
{
notifyIcon.Dispose();
notifyIcon = null;
}
base.Dispose(disposing);
}
回答by Ibraheem Osama
This code worked for me
这段代码对我有用
this.Closed += (a, b) =>
{
if (notifyIcon1 != null)
{
notifyIcon1.Dispose();
notifyIcon1.Icon = null;
notifyIcon1.Visible = false;
}
};
回答by Emin Yal??n
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
在我为我的英语不好而感到抱歉之前。如果您使用“结束”退出程序。然后不要关闭通知图标。在您关闭notifyicon 之前关闭表单。你需要使用 me.close() 来关闭运行表单
example its work...
例如它的工作......
notifyIcon1.Icon = Nothing
notifyIcon1.Visible = False
notifyIcon1.Dispose()
Me.Close()
but its not work
但它不起作用
End
or only
或仅
Me.Close()