My app needs to show balloon notifications from time to time. I do this via Dispatcher.Invoke()
that performs an Action
that creates a NotifyIcon
, displays the balloon notification, then disposes the system tray icon.
public abstract class Foo {
void Bar() {
MainWindow.ShowTrayNotification(ToolTipIcon.Info, "Hello There!", "Balloon text here.");
}
}
public partial class MainWindow : Window {
static Dispatcher dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
public static void ShowTrayNotification(ToolTipIcon icon, string title, string text) {
dispatcher.Invoke(new Action(() => {
//anything here is not run
UIHelper.ShowTrayNotification(icon, title, text); //static method
}));
//anything here is also not called if dispatcher.Invoke() is not run
}
}
However, sometimes the Dispatcher.Invoke
is not run. Logging stops right before it, and no logging happens inside the Invoke
's Action
.
What's weird is that when I trigger a PC shutdown, suddenly the Invoke
is fired (the notification is shown) and proceeds normally (my pre-shutdown callbacks are performed).
I tried to do Debug > Windows > Threads, but the issue does not happen in Debug mode, so I can not find what is causing the UI thread to block.
What is causing this and how can I fix it?
UPDATE
I tried the following and they didn't work:
I changed it to
BeginInvoke
.I added
DispatcherPriority.Send
.
UPDATE 2
public class UIHelper
{
public static void ShowTrayNotification(ToolTipIcon icon, string title, string text) {
NotifyIcon trayIcon = new NotifyIcon();
trayIcon.Icon = MyApp.Properties.Resources.myIcon;
trayIcon.Text = "MyApp";
trayIcon.BalloonTipClosed += TrayNotificationClosed;
trayIcon.Visible = true;
trayIcon.ShowBalloonTip(5000, title, text, icon);
}
static void TrayNotificationClosed(object sender, EventArgs e) {
((NotifyIcon)sender).Visible = false;
((NotifyIcon)sender).Icon = null;
((NotifyIcon)sender).Dispose();
sender = null;
}
}