title: How to chain independent C# tasks?
body: <p>Let's say I have two independent async functions (that I don't control) that create Task objects:</p>
  
  <pre><code>Task A();
  Task B();
  </code></pre>
  
  <p>and some other non-async function</p>
  
  <pre><code>void X();
  </code></pre>
  
  <p>How do I construct a single Task chain that executes all of these in sequence and allows further continuations (that will execute after X) to be appended?</p>
  
  <p>If I do this:</p>
  
  <pre><code>Task Sequential()
  {
     return A()
       .ContinueWith(t =&gt; { B(); })
       .ContinueWith(t =&gt; { X(); });
  }
  </code></pre>
  
  <p>that does not work, because B will start a new Task chain. If B takes a long time to complete, X will be executed first (along with whatever else the caller of Sequential might ContinueWith on the returned Task). I need X to be the last element in a single Task chain, with the B Task as its precedent.</p>
  
  <p>If I do this:</p>
  
  <pre><code>Task Sequential()
  {
     return A()
       .ContinueWith(t =&gt; { B().ContinueWith(t =&gt; { X(); }); });
  }
  </code></pre>
  
  <p>that only partially solves the problem, because even though A, B and X will now execute in sequential order, if the caller does Sequential().ContinueWith, that continuation will execute in parallel to B and X, not after X.</p>