title: Task.Wait vs Task.RunSyncronously where task has call to WPF Dispatcher.Invoke
body: <p>I have a <code>Task</code> that I am starting and wish to wait for completion in a WPF app. Inside this task I invoke an <code>Action</code> on the dispatcher.</p>
  
  <p>If I use <code>Task.Wait()</code> it appears to hang as if the method never finished. Also, breakpoints inside the Dispatcher.Invoke are never hit.</p>
  
  <p>If I use <code>Task.RunSyncronously()</code> it appears to work correctly and breakpoints inside the Dispatcher are hit.</p>
  
  <p>Why is there a difference?</p>
  
  <p>Code sample below:</p>
  
  <pre><code>public void ExampleMethod()
  {
      // When doing the following:
      var task = new Task(LoadStuff);
  
      // This never returns:
      task.Start();
      task.Wait();
  
      // This version, however, does:
      task.RunSyncronously();
  }
  
  private void LoadStuff()
  {
      ObservableCollection&lt;StuffObj&gt; stuff = Stuff.Load(arg1, true);
  
      DispatchHelper.RunOnDispatcher(() =&gt;
      {
          ...
      });
  }
  
  public static class DispatchHelper
  {
      public static void RunOnDispatcher(Action action)
      {
          Application.Current.Dispatcher.Invoke(action);
      }
  }    
  </code></pre>