title: Displaying WPF-"NotifyIcon" on a separate thread
body: <p>I am currently working on an office add-in and I need to show a notification dialog that displays progress, I'm using <a href="http://www.codeproject.com/Articles/36468/WPF-NotifyIcon" rel="nofollow">Philipp Sumi's wpf-notifyicon</a>.</p>
  
  <p>I need to display the <a href="http://www.codeproject.com/Articles/36468/WPF-NotifyIcon" rel="nofollow">notifyicon</a> from a separate thread as I have a lot of code that already executes on the main thread, this causes the wpf-notifyicon to block and wait because the messages in the windows message queue are not being processed.</p>
  
  <p>I know that I should rather execute this time consuming code on a separate thread, and display the notifyicon from the main thread and update it accordingly, but that is unfortunately not an alternative because this whole solution is single-threaded.</p>
  
  <p>Example:</p>
  
  <pre><code>    private FancyPopup fancyPopup;
  
      private void button1_Click(object sender, EventArgs e)
      {
          notifyIcon = new TaskbarIcon();
          notifyIcon.Icon = Resources.Led;
  
          fancyPopup = new FancyPopup();
  
          Thread showThread = new Thread(delegate()
          {
              notifyIcon.ShowCustomBalloon(fancyPopup, System.Windows.Controls.Primitives.PopupAnimation.Fade, null);
          });
  
          showThread.Start();
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          fancyPopup.TextB.Text = "Doing something...";
  
          //Keep the main thread busy.
          Thread.Sleep(5000);
  
          fancyPopup.TextB.Text = "Done doing something...";
      }
  </code></pre>
  
  <p><strong>Update</strong>
  I have been able to progress a little further with this updated code:</p>
  
  <p>I'm creating the TaskbarIcon object on a new thread , and using Application.Run to process the application message loop on that thread...</p>
  
  <pre><code>    private FancyPopup fancyPopup;
  
      private void button1_Click(object sender, EventArgs e)
      {
          Thread showThread = new Thread(delegate()
          {
              notifyIcon = new TaskbarIcon();
              notifyIcon.Icon = Resources.Led;
  
              fancyPopup = new FancyPopup();
  
  
              notifyIcon.ShowCustomBalloon(fancyPopup, System.Windows.Controls.Primitives.PopupAnimation.Fade, null);
  
              System.Windows.Forms.Application.Run();
          });
  
          showThread.SetApartmentState(ApartmentState.STA);
          showThread.Start();
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          fancyPopup.Dispatcher.Invoke(new Action(delegate
          {
              fancyPopup.TextB.Text = "Doing something...";
          }));
  
          //Keep the main thread busy.
          Thread.Sleep(5000);
  
          fancyPopup.Dispatcher.Invoke(new Action(delegate
          {
              fancyPopup.TextB.Text = "Done doing something..."; 
          }));
      }
  </code></pre>