title: Display "Busy Indicator" using background thread
body: <p>I am doing some heavy computations on the main thread and these computations cannot run on a separate thread.</p>
  
  <p>I want to display a "Busy Indicator" (i.e., spinning widget) on the application UI when these computations are running. As such, I cannot show the busy indicator on main thread as the UI is locked while these computations are running.</p>
  
  <p>To work around this issue, I tried to move the busy indicator to separate thread. With the help of <a href="http://blogs.msdn.com/b/dwayneneed/archive/2007/04/26/multithreaded-ui-hostvisual.aspx" rel="nofollow">this post</a> I am able to place the busy indicator on separate thread. However, I am not able to communicate with this thread to start or stop the busy indicator.</p>
  
  <pre><code>     private HostVisual CreateBusyIndicatorOnWorkerThread()
  
      {
          // Create the HostVisual that will "contain" the VisualTarget
          // on the worker thread.
          HostVisual hostVisual = new HostVisual();
          Thread thread = new Thread(new ParameterizedThreadStart(BusyIndicatorWorkerThread));
          thread.ApartmentState = ApartmentState.STA;
          thread.IsBackground = true;
          thread.Start(hostVisual);
          // Wait for the worker thread to spin up and create the VisualTarget.
          s_event.WaitOne();
          return hostVisual;
      }
  
   private static AutoResetEvent s_event = new AutoResetEvent(false);
   private void BusyIndicatorWorkerThread(object arg)
  
      {
          // Create the VisualTargetPresentationSource and then signal the
          // calling thread, so that it can continue without waiting for us.
          HostVisual hostVisual = (HostVisual)arg;
          VisualTargetPresentationSource visualTargetPS = new VisualTargetPresentationSource(hostVisual);
          s_event.Set();
  
          // Create a MediaElement and use it as the root visual for the
          // VisualTarget.
          visualTargetPS.RootVisual = CreateBusyIndicator();
  
          // Run a dispatcher for this worker thread.  This is the central
          // processing loop for WPF.
          System.Windows.Threading.Dispatcher.Run();
      }
  
    private FrameworkElement CreateBusyIndicator()
  
      {
          var busyIndicator = new MyBusyIndicator();
          //busyIndicator.DataContext = this.
          Binding myBinding = new Binding("IsBusy");
          myBinding.Source = this;
          busyIndicator.SetBinding(MyBusyIndicator.IsBusyProperty, myBinding);
       }
  </code></pre>
  
  <p>I always gets an exception "The calling thread cannot access this object because a different thread owns it". This is because I am trying to update the busy indicator from the main thread while the busy indicator is owned by a different thread.</p>
  
  <p>I have also tried an approach given in <a href="http://abrahamheidebrecht.wordpress.com/2009/08/05/creating-a-busy-indicator-in-a-separate-thread-in-wpf/" rel="nofollow">this article</a>, </p>
  
  <pre><code>private void CreateAndShowContent()
      {
          Dispatcher = Dispatcher.CurrentDispatcher;
          VisualTargetPresentationSource source =
              new VisualTargetPresentationSource(_hostVisual);
          _sync.Set();
          source.RootVisual = _createContent();
          DesiredSize = source.DesiredSize;
          _invalidateMeasure();
  
          Dispatcher.Run();
          source.Dispose();
      }
  </code></pre>
  
  <p>But with this approach Dispatcher.Run() nothing happens until after the completion of the computations and then the busy indicator is displayed.</p>
  
  <p>I want to communicate from main thread to the thread having busy indicator. Does anyone have an approach?</p>