4

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 ?

H H
  • 263,252
  • 30
  • 330
  • 514
BuZz
  • 16,318
  • 31
  • 86
  • 141

1 Answers1

4

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.

ChrisBD
  • 9,104
  • 3
  • 22
  • 35
  • Thanks Chirs, the effect of this is the one desired. However, my application becomes instable after just a couple bubbles. Maybe because this is synchroneous after all ? – BuZz Nov 23 '11 at 13:12
  • Try running the timer in a worker thread that uses a delegate or method to use Invoke to change the Icon visibility. – ChrisBD Nov 23 '11 at 14:15
  • 1
    Appears to be true for Windows 7 (the balloon tip stays until there's user input), but not for Windows 10, so I guess this is also OS specific. – TH Todorov Sep 25 '15 at 09:55