title: System.InvalidOperationException and System.Reflection.TargetInvocationException occasionally happening
body: <p>When running my program, sometimes I get these exceptions, other times I don't. This is the flow of execution:</p>
  
  <p>User clicks a button in the Window1 class.<br>
  Class collects inputs, backgroundWorker.RunWorkerAsync() is started.<br>
  In the DoWork, App.doStuff() is called.<br>
  Inside App.doStuff(), I create another thread to show a progressbar in a different window like this:</p>
  
  <pre><code>Thread newWindowThread =  new Thread(new ThreadStart(showProgressMethod));
  newWindowThread.SetApartmentState(ApartmentState.STA);
  newWindowThread.IsBackground = true;
  newWindowThread.Start();
  </code></pre>
  
  <p>In the showProgressMethod(), I do:</p>
  
  <pre><code>progressWindow = new MyProgressWindow(this);
  progressWindow.Show();
  System.Windows.Threading.Dispatcher.Run();
  </code></pre>
  
  <p>Now, if I'm running it for the first time, I never have any problems. But when/if I redo the operation once it's completed, then I might get the exceptions.</p>
  
  <p>Looking at the code, I get the feeling I'm doing something wrong by calling the dispatcher.Run() and the Thread.Start(), but I don't understand the threading model well, so I'm not sure.</p>
  
  <p>Either way, on the first run, the program never does anything unusual. But sometimes when I click the button again to activate the process, then the exception occurs. I think it happens when the program hasn't had enough time to clean up? I'm not sure though.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Seems that the problem is that the ProgressWindow thread isn't ending. Apparently adding a Thread.Abort() inside App.doStuff() or Dispatcher.Thread.Abort() inside the ProgressWindow fixes the problem. Apparently.</p>
  
  <p>But, it's still raising exceptions because of the Abort() method, though it no longer crashes. I also don't understand why closing the ProgressWindow doesn't end the thread. And from what I've read using Thread.Abort() isn't good practice though again I don't understand why.</p>
  
  <p><strong>EDIT 2</strong>  </p>
  
  <p>Thread.Abort still had it crashing at times. So, what I've done is:<br>
  Replace the <code>Show()</code> call for <code>ShowDialog()</code>, as suggested and<br>
  Remove the call for the <code>System.Windows.Threading.Dispatcher.Run();</code></p>