0

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?

Community
  • 1
  • 1
Drew
  • 12,578
  • 11
  • 58
  • 98
  • As a side note, I had some issues with NotifyIcons in C# - one of the "inconsistencies" i came accross was solved by forcing the Visible property to false then true i.e. to "refresh" the system tray – Simon Mar 06 '11 at 23:48
  • Thanks, unfortunately setting it to invisible then visible didn't help. – Drew Mar 07 '11 at 00:11
  • It's kind of a strange request. Windows will not show a balloon if the user is away from the keyboard - and will delay showing it until they get back ("so as to maximimize the amount of face-time your balloon gets with the user"). If the application is idle, then almost by definition the user is not there, which is exactly the time you don't want to show a balloon (which is okay since Explorer won't show it anyway). But i'll agree with ChrisF, set a flag. – Ian Boyd Mar 07 '11 at 00:20

2 Answers2

1

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thats unfortunate, I have having to use Thread.Sleep() to take care of race conditions. – Drew Mar 07 '11 at 02:27
0

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;
}
ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • This is not exactly my problem, I have it set up so it only shows the balloon once, but if the NotifyIcon is not visible, the balloon won't appear. I need a way to force the NotifyIcon to appear. – Drew Mar 07 '11 at 00:09
  • @Drew - Ah. I was assuming that there was just some problem in getting the events to happen in the right order. Setting the `notifyIcon.Visible` to `true` should make it appear. – ChrisF Mar 07 '11 at 00:12