Drew
Drew

Reputation: 13418

Force NotifyIcon to appear in system tray

I have a NotifyIcon that appears in the system tray and I want to show a balloon tip the first time stuff the application is idle (As suggested here: C# execute code after application.run() ) but the Idle event happens before the Icon appears in the System tray, causing the balloon to not appear. How can I force the NotifyIcon to appear before I call ShowBalloonTip?

Upvotes: 0

Views: 2965

Answers (2)

Hans Passant
Hans Passant

Reputation: 942408

This is a fairly fundamental race, it is another process that takes care of the icon. Windows Explorer. You can't tell when it took care of things. Calling Thread.Sleep(500) after setting Visible = true ought to improve the odds significantly.

Do consider displaying the icon when your program starts.

Upvotes: 1

ChrisF
ChrisF

Reputation: 137188

Why not set a flag on idle and then check the state of the flag after setting the notify icon to visible:

// Application has become idle
firstTimeIdle = true;

Then:

// Show notify icon
notifyIcon.Visible = true;
if (firstTimeIdle && !shownBalloon)
{
    notifyIcon.ShowBalloonTip(timeout, title, text, icon);
    shownBalloon = true;
}

Upvotes: 0

Related Questions