title: Application.Current.Dispatcher.Invoke() from within an action
body: <p>i have a problem where I run part of my code using the <code>Dispatcher</code> class from <code>Application.Current</code> namespace. Then I want to construct a follow up operation using the <code>ContinueWith</code> method of the task returned by <code>Dispatcher.Invoke()</code>.</p>
  
  <p>In this particular case, the follow up operation needs to be run from within the UI thread aswell, so it needs to be wrapped in the <code>Dispatcher.Invoke</code> again. This is how I got it to work:</p>
  
  <pre><code>Action doSomeMoreStuff = () =&gt; { this.MoreStuff; }
  Application.Current.Dispatcher.Invoke(() =&gt; this.DoStuff).ContinueWith(x =&gt; Application.Current.Dispatcher.Invoke(this.DoSomeMoreStuff));
  </code></pre>
  
  <p>I want to keep it generic however, and there might be situations where I don't want the follow up code to be run from within the UI thread. So I tried to encapsulate the follow up code itself:</p>
  
  <pre><code>Action doSomeMoreStuff = () =&gt; { Application.Current.Dispatcher.Invoke(this.MoreStuff); }
  Application.Current.Dispatcher.Invoke(() =&gt; this.DoStuff).ContinueWith(x =&gt; this.DoSomeMoreStuff);
  </code></pre>
  
  <p>So as far as I understand this problem, I simply switched the position of the <code>Application.Current.Dispatcher.Invoke()</code> call. However, the second approach does not work, the code does not get called, and I have no idea why.</p>
  
  <p>What am I not getting here?</p>