C# NofityIcon 气球提示并不总是在指定时间内消失

发布于 2024-12-17 12:40:37 字数 607 浏览 2 评论 0原文

我以相当简单的方式使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2024-12-24 12:40:37

造成此行为的部分原因是 ShowBalloonToolTip 中使用的计时器设计为仅在操作系统检测到用户输入时运行。因此,如果您只是等待气球消失而不实际执行任何操作,那么它永远不会超时。

我相信原因是,如果您离开电脑并在一小时后回来,那么您就不会错过任何通知。

有一种解决方法,那就是运行一个单独的计时器来切换图标的可见性。

例如:

private void ShowBalloonWindow(int timeout)
        {
            if (timeout <= 0)
                return;

            int timeoutCount = 0;
            trayIcon.ShowBalloonTip(timeout);

            while (timeoutCount < timeout)
            {
                Thread.Sleep(1);
                timeoutCount++;
            }

            trayIcon.Visible = false;
            trayIcon.Visible = true;
        }

编辑

啊是的 - 我把它拼凑在一起,没有考虑你是如何使用它的。
如果您希望异步运行此程序,那么我建议您将计时器放置在一个工作线程中,该线程调用一个方法,该方法在完成时切换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:

private void ShowBalloonWindow(int timeout)
        {
            if (timeout <= 0)
                return;

            int timeoutCount = 0;
            trayIcon.ShowBalloonTip(timeout);

            while (timeoutCount < timeout)
            {
                Thread.Sleep(1);
                timeoutCount++;
            }

            trayIcon.Visible = false;
            trayIcon.Visible = true;
        }

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 the trayIcon.Visible property on completion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文