I have an application that runs a new thread to show a taskbar icon. Now I just can’t figure out how I can call the TaskbarIcon (this is created on the new thread) from my main thread to show a balloon tip.
The code I have right now is:
public class NotificationHelper
{
private TaskbarIcon notifyIcon { get; set; }
public NotificationHelper()
{
Thread thread = new Thread(OnLoad);
thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
thread.Start();
}
public void ShowNotification(string text)
{
notifyIcon.ShowBalloonTip("Demo", text, notifyIcon.Icon);
}
public void OnLoad()
{
notifyIcon = new TaskbarIcon();
notifyIcon.Icon =
new Icon(@".\Icon\super-man-icon.ico");
//notifyIcon.ToolTipText = "Left-click to open popup";
notifyIcon.Visibility = Visibility.Visible;
while (true)
{
Thread.Sleep(1000);
}
}
private void ShowBalloon()
{
notifyIcon.ShowBalloonTip("Demo", Message, notifyIcon.Icon);
}
}
And when I try to call ‘ShowNotification(“foobar”);’ I get this exception:
Object reference not set to an instance of an object.
The reason why I have ‘while(true){}’ in ‘Onload()’ is that I need the thread to be running until I close my application.
In your main thread, create a dispatcher with:
Pass it to your NotificationHelper:
Show the balloon: