C# NofityIcon 气球提示并不总是在指定时间内消失
我以相当简单的方式使用 NotifyIcon。
public class Popup
{
...
private static NotifyIcon ni;
static Popup()
{
ni = new NotifyIcon();
ni.Icon = SystemIcons.Information;
}
public Popup(string nexusKey)
{
...
}
public void make(string text)
{
try
{
...
}
catch
{
ni.Visible = true;
ni.ShowBalloonTip(1000, "Thats the title", text, ToolTipIcon.Info);
}
}
}
问题是,如果我关注的窗口与托管显示气球的进程的窗口不同,“保持活动”计时器似乎不会启动。关于如何确保气球在 1 秒后无论如何都消失有什么想法吗?
I use a NotifyIcon in a rather simple fashion.
public class Popup
{
...
private static NotifyIcon ni;
static Popup()
{
ni = new NotifyIcon();
ni.Icon = SystemIcons.Information;
}
public Popup(string nexusKey)
{
...
}
public void make(string text)
{
try
{
...
}
catch
{
ni.Visible = true;
ni.ShowBalloonTip(1000, "Thats the title", text, ToolTipIcon.Info);
}
}
}
Problem is, it seems like the "stay alive" timer doesn't get started if I am focusing different windows than the one hosting the process that display the balloon. Any ideas on how to make sure the balloon goes away after 1 second no matter what ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
造成此行为的部分原因是 ShowBalloonToolTip 中使用的计时器设计为仅在操作系统检测到用户输入时运行。因此,如果您只是等待气球消失而不实际执行任何操作,那么它永远不会超时。
我相信原因是,如果您离开电脑并在一小时后回来,那么您就不会错过任何通知。
有一种解决方法,那就是运行一个单独的计时器来切换图标的可见性。
例如:
编辑
啊是的 - 我把它拼凑在一起,没有考虑你是如何使用它的。
如果您希望异步运行此程序,那么我建议您将计时器放置在一个工作线程中,该线程
调用
一个方法,该方法在完成时切换trayIcon.Visible
属性。Part of the reason for this behaviour is that the timer used in ShowBalloonToolTip was designed to only run when the OS detects user input. Thus if you are just waiting for the balloon to disappear and not actually doing anything then it will never timeout.
I believe that the reasoning was that if you left your PC and came back an hour later then you wouldn't miss any notifications.
There is a way around it, and that is to run a separate timer that toggles the icon visibility.
For example:
edit
Ah yes - I cobbled that together without thinking about how you were using it.
If you wish to run this asynchronously then I'd suggest that you place the timer within a worker thread that
Invokes
a method that toggles thetrayIcon.Visible
property on completion.