title: How to await until Task.ContinueWith inner task finishes
body: <p>Take a look at this code:</p>
  
  <pre><code>    private async Task InnerTask(bool outerTaskResult)
      {
          Console.WriteLine("2");
          await Task.Factory.StartNew(() =&gt; Thread.Sleep(10000));
          Console.WriteLine("3");
      }
  
      private async void Button2_Click(object sender, RoutedEventArgs e)
      {
          var task = Task.FromResult(false);
          Task&lt;Task&gt; aggregatedTask = task.ContinueWith(task1 =&gt; InnerTask(task1.Result));
          Console.WriteLine("1");
          await aggregatedTask;
          Console.WriteLine("4");
      }
  </code></pre>
  
  <p>The desired output is:</p>
  
  <pre><code>1
  2
  3
  4
  </code></pre>
  
  <p>But I get:</p>
  
  <pre><code>1
  2
  4
  3
  </code></pre>
  
  <p>This probably has something to do with InnerTask being executed on a different thread.</p>
  
  <p>I'm using ContinueWith because the tasks in the original code are dynamically created and queued this way.</p>
  
  <p>Using <em>.Wait()</em> method (see below) works, but I think it's a bad idea, as the method is blocking.</p>
  
  <pre><code>task.ContinueWith(task1 =&gt; InnerTask(task1.Result).Wait())
  </code></pre>
  
  <p>What's the correct approach here?</p>