For the sake of future coders:
the [timeout] parameter is deprecated as of windows vista
See: C# NotifyIcon Show Balloon Parameter Deprecated
So you might as well just put 0 into the parameter for > Windows Vista. What's worse, comments on the linked answer suggests that the replacement for these balloons, toast notifications, were only introduced in Windows 8. So for poor old Windows 7 falling between two stools, with Vista < 7 < 8, we seem to be at the mercy of however long Windows wants to keep that balloon there! It does eventually fade away, I've noticed, but after some empirical testing I'm quite sure that parameter is indeed being ignored.
So, building on the answers above, and in particular taking the lambda functions suggested by @jlmt in the comments, here's a solution that works for me on Windows 7:
//Todo: use abstract factory pattern to detect Windows 8 and in that case use a toastnotification instead
private void DisplayNotificationBalloon(string header, string message)
{
NotifyIcon notifyIcon = new NotifyIcon
{
Visible = true,
Icon = SystemIcons.Application
};
if (header != null)
{
notifyIcon.BalloonTipTitle = header;
}
if (message != null)
{
notifyIcon.BalloonTipText = message;
}
notifyIcon.BalloonTipClosed += (sender, args) => dispose(notifyIcon);
notifyIcon.BalloonTipClicked += (sender, args) => dispose(notifyIcon);
notifyIcon.ShowBalloonTip(0);
}
private void dispose(NotifyIcon notifyIcon)
{
notifyIcon.Dispose();
}
Notes
- I've put a TODO in there to write another implementation for Windows
8, as people are 50/50 now on Windows 7/8 so would be good to support
a newer functionality. I guess anyone else coding this for multiple
versions of windows should probably do the same, ideally. Or just
stop supporting 7 and switch to using ToastNotification.
- I purposely defined the disposal in a function so I could debug and verify that the breakpoint was indeed being hit.
Dispose
on Window Closing/Closed, otherwise it lingers until you move the mouse over it. – Fronia