Winforms 应用程序上 NotifyIcon 未消失的问题
我有一个 .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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 Windows 7 上,我还必须将 Icon 属性设置为 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.
这段代码对我有用,但我不知道你如何让你的应用程序保持活动状态,所以......不用多说:
This code works for me, but I don't know how you are keeping your application alive, so... without further ado:
这就是我在 WPF 中所做的。
我将其与 David Anson 的 最小化到托盘示例应用,它可以让您将托盘图标连接到窗口(您可能有多个窗户打开)。
刚刚将此代码添加到
MinimizeToTrayInstance
的构造函数中。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
.有时 Application_Exit 事件可能会引发多次
只要把notifyIcon = null; 到底
Sometimes Application_Exit event can be raised several times
Just put notifyIcon = null; in the end
这段代码对我有用
This code worked for me
您是否重写了初始化notifyIcon的对象的dispose方法以也处置notifyIcon?
Have you overridden the dispose method of the object where you've initialised the notifyIcon to also dispose the notifyIcon?
在我为我糟糕的英语感到抱歉之前。
如果你使用“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...
but its not work
or only