title: Ensuring that things run on the UI thread in WPF
body: <p>I'm building a WPF application. I'm doing some async communication with the server side, and I use event aggregation with Prism on the client. Both these things results in new threads to be spawned which are not the UI thread. If I attempt to do "WPF operations" on these callback and event handler threads the world will fall apart, which it now has started doing. </p>
  
  <p>First I met problems trying to create some WPF objects in the callback from server. I was told that the thread needed to run in STA mode. Now I'm trying to update some UI data in a Prism event handler, and I'm told that: </p>
  
  <blockquote>
    <p>The caller cannot access this thread because a different thread owns it.</p>
  </blockquote>
  
  <p>So; <strong>what's the key to getting things right in WPF?</strong> I've read up on the WPF Dispatcher in <a href="http://msdn.microsoft.com/en-us/magazine/cc163328.aspx" rel="noreferrer">this MSDN post</a>. I'm starting to get it, but I'm no wizard yet. </p>
  
  <ol>
  <li>Is the key to always use Dispatcher.Invoke when I need to run something which I'm not sure will be called on the UI thread? </li>
  <li>Does it matter if it actually was called on the UI thread, and I do Dispatcher.Invoke anyway? </li>
  <li>Dispatcher.Invoke = synchronously. Dispathcher.BeginInvoke = async? </li>
  <li>Will Dispatcher.Invoke request the UI thread, and then stop to wait for it? Is it bad practice and risk of less responsive programs? </li>
  <li>How do I get the dispatcher anyway? Will Dispatcher.CurrentDispatcher always give me the dispatcher representing the UI thread? </li>
  <li>Will there exist more than one Dispatcher, or is "Dispatcher" basically the same as the UI thread for the application? </li>
  <li>And what's the deal with the BackgroundWorker? When do I use this instead? I assume this is always async? </li>
  <li>Will everything that runs on the UI thread (by being Invoked) be run in STA apartment mode? I.e. if I have something that requires to be run in STA mode - will Dispatcher.Invoke be sufficient? </li>
  </ol>
  
  <p>Anyone wanna clearify things for me? Any related recommendations, etc? Thanks! </p>