C # NofityIcon ball tip doesn't always go at a specified time

I use NotifyIcon in a fairly simple way.

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); } } } 

The problem is that the โ€œstay aliveโ€ timer does not start if I focus on different windows than the one that hosts the process that displays the balloon. Any ideas on how to make sure the balloon leaves after 1 second no matter what?

+4
source share
1 answer

Part of the reason for this behavior is that the timer used in ShowBalloonToolTip was designed to work only when the OS detects user input. That way, if you just expect the balloon to disappear and actually do nothing, it will never time out.

I believe the reasoning was that if you left your computer and returned in an hour, you would not miss any notifications.

There is a way around this, and this is to start a separate timer that switches the visibility of the icon.

For instance:

 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; } 

change

Oh yes, I put it together without thinking about how you used it. If you want to run this asynchronously, I would suggest that you put a timer in the workflow to the Invokes method, which by default completes the trayIcon.Visible property.

+4
source

Source: https://habr.com/ru/post/1382681/


All Articles