title: How do I write code to realize these tasks?
body: <p>I want to realize some async code in C# but I can't write. Give me a hint.</p>
  
  <p>I have 4 tasks (task 0, task 1, task 2, task 3). </p>
  
  <ul>
  <li>Task 1 is after task 0.</li>
  <li>Task 3 is after task 2. </li>
  <li>Task 0 and Task 2 can be parallel. </li>
  <li>Task 1 and task 3 is done only one by one, that is, task 1 after task 3 or task 3 after task 1.</li>
  </ul>
  
  <p>Then, how can I realize these tasks in C# code?</p>
  
  <p>The following code is imperfect because Task 1 and task 3 should not be pallalel.</p>
  
  <pre><code>static Task WholeTask()
  {
      var tasks = new List&lt;Task&gt;();
      tasks.Add(Task0and1());
      tasks.Add(Task2and3());
      return Task.WhenAll(tasks);
  }
  
  static async Task Task0and1()
  {
      await Task.Run(() =&gt; Method0());
      await Task.Run(() =&gt; Method1());
  }
  
  static async Task Task2and3()
  {
      await Task.Run(() =&gt; Method2());
      await Task.Run(() =&gt; Method3());
  }
  
  static void Method0()
  {
      Thread.Sleep(1000);
      Console.WriteLine("task 0 is completed");
  }
  static void Method1()
  {
      Console.WriteLine("task 1 starts");
      Thread.Sleep(1000);
      Console.WriteLine("task 1 is completed");
  }
  static void Method2()
  {
      Thread.Sleep(1000);
      Console.WriteLine("task 2 is completed");
  }
  static void Method3()
  {
      Console.WriteLine("task 3 starts");
      Thread.Sleep(1000);
      Console.WriteLine("task 3 is completed");
  }
  static void Main(string[] args)
  {
      WholeTask().Wait();
  }
  </code></pre>