Reputation: 17425
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 ?
Upvotes: 4
Views: 3130
Reputation: 9209
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.
Upvotes: 4