title: How to keep ProgressBar Indeterminate animating
body: <p>I've been learning about how this issue is confronted via Threads (<code>BackgroundWorker</code> and others) and how to keep the UI responsive. I've managed to do so but then I realized that is not what I want. What I need actually is just to display an animating <code>ProgressBar</code> while a long operation is performed. (I don't need/want to update the UI because in that operation I'm exporting all graphical representation and the plotter is updated constantly).</p>
  
  <p>I have thought about a Dialog popping-up with the progress bar while the operation is being performed. The problem is that I get this exception:</p>
  
  <pre><code>The calling thread cannot access this object because a different thread owns it
  </code></pre>
  
  <p>There are plenty of questions about this and the answer is to use <code>Dispatcher</code>:</p>
  
  <pre><code>            Dispatcher.CurrentDispatcher.Invoke(() =&gt; myOperation(), DispatcherPriority.Normal);
  </code></pre>
  
  <p>Here is what've done:</p>
  
  <p>I'm using Modern UI, there is a Dialog called ModernDialog, it's just a fancy dialog:</p>
  
  <pre><code>  class DialogProgress
  {
      public ModernDialog progressDlg = new ModernDialog();
  
      public DialogProgress()
      {
          ProgressBar bar = new ProgressBar();
          bar.IsIndeterminate = true;
          bar.Width = 150;
          StackPanel content = new StackPanel();
          content.Children.Add(bar);
          progresoDlg.Content = content ;
  
          //Thread paralel = new Thread(() =&gt; myOperation());
          //paralel.SetApartmentState(ApartmentState.STA);
          //paralel.Start();
          Dispatcher.CurrentDispatcher.Invoke(() =&gt; myOperation(), DispatcherPriority.Normal);
      }
  
      void myOperation()
      {
          progresoDlg.ShowDialog();
      }
  }
  </code></pre>
  
  <p>I know I'm mixing stuff there, there are Threads and Dispatcher, but I can't figure out how to use them.</p>
  
  <p>Here is how I call this dialog:</p>
  
  <pre><code>public void MyLongMethod()
  {
          DialogProgress progress = new DialogProgress();
  }
  </code></pre>
  
  <p>If I use only Dispatcher, the dialog shows up and the bar is being animated but MyLongMethod is not working (it starts after closing the dialog).</p>
  
  <p>If I use Threads I get the mentioned exception.</p>
  
  <p>How can I solve this problem?</p>
  
  <p>(P.D. using a dialog Is only a suggestion, I would be happy too if the progress bar is in the UI and I switch the visibility when the long method starts/ends)</p>