title: How to make one task run after a collection of tasks has finished in C#?
body: <p>I have a collection of tasks that have to finish before one last task is run. Specifically:</p>
  <pre><code>protected override async Task ExecuteAsync(CancellationToken stoppingToken)
      {
          var task1 = Task.Run(() =&gt;
          {
              Thread.Sleep(5000);
              Console.WriteLine(&quot;Task1&quot;);
          });
  
          var task2 = Task.Run(() =&gt;
          {
              Thread.Sleep(5000);
              Console.WriteLine(&quot;Task2&quot;);
          });
  
          var task3 = Task.Run(() =&gt;
          {
              Thread.Sleep(5000);
              Console.WriteLine(&quot;Task2&quot;);
          });
  
          var last = Task.Run(() =&gt;
          {
              Console.WriteLine(&quot;Last&quot;);
          });
  
          var tasks = new List&lt;Task&gt;();
          tasks.Add(task1);
          tasks.Add(task2);
          tasks.Add(task3);
  
          await Task.WhenAll(tasks).ContinueWith(t =&gt; last);
      }
  </code></pre>
  <p>Currently they finish like this:</p>
  <pre><code>Last
  Task2
  Task3
  Last1
  </code></pre>
  <p>I want them to finish like this:</p>
  <pre><code>Task1
  Task2
  Task3
  Last
  </code></pre>
  <p>The order of first 3 does not matter, what matters is that <strong>Last</strong> task finishes last.
  I cannot block the thread or wait for the collection to finish and similar stuff, only the <strong>Last</strong> task has to perform after first three are done.</p>