简体   繁体   中英

Access object created on new thread

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:

Dispatcher dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;

Pass it to your NotificationHelper:

Dispatcher FDispatcher;

public NotificationHelper(Dispatcher ADispatcher)
{
     FDispatcher = ADispatcher;
     //...
}

Show the balloon:

private void ShowBalloon()
{
    FDispatcher.invoke(new Action(() => {
        notifyIcon.ShowBalloonTip("Demo", Message, notifyIcon.Icon);
    }));
}

You could try locking notifyIcon and checking for null like this:

public class NotificationHelper 
{
    private readonly object notifyIconLock = new object();
    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)
    {
        lock (notifyIconLock)
        {
            if (notifyIcon != null)
            {
                notifyIcon.ShowBalloonTip("Demo", text, notifyIcon.Icon);
            }
        }
    }

    public void OnLoad()
    {
        lock (notifyIconLock)
        {
            notifyIcon = new TaskbarIcon();
            notifyIcon.Icon =
                new Icon(@".\Icon\super-man-icon.ico");
            //notifyIcon.ToolTipText = "Left-click to open popup";
            notifyIcon.Visibility = Visibility.Visible;
        }
    }

    private void ShowBalloon()
    {
        lock (notifyIconLock)
        {
            if (notifyIcon != null)
            {
                notifyIcon.ShowBalloonTip("Demo", Message, notifyIcon.Icon);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM