title: How do I create a Task that uses await inside the body that behaves the same as the synchronous version when Wait is called?
body: <p>I have some code that creates a task that does some slow work like this:</p>
  
  <pre><code>public static Task wait1()
  {
      return new Task(() =&gt;
      {
          Console.WriteLine("Waiting...");
          Thread.Sleep(10000);
          Console.WriteLine("Done!");
      });
  }
  </code></pre>
  
  <p>In the real implementation, the Thread.Sleep will actually be a web service call. I would like to change the body of the method can use await (so it does not consume a thread during the network access/sleep). My first attempt (based on shotgun-debugging the compile errors) was this:</p>
  
  <pre><code>public static Task wait2()
  {
      return new Task(async () =&gt;
      {
          Console.WriteLine("Waiting...");
          await Task.Delay(10000);
          Console.WriteLine("Done!");
      });
  }
  </code></pre>
  
  <p>However; this task doesn't seem to behave the same as the first one, because when I call .Wait() on it; it returns immediately.</p>
  
  <p>Below is a full sample (console app) showing the differences (the app will end immediately when the second task starts).</p>
  
  <p>What do I need to do so that I can call Start and Wait on a Task which happens to have code using await inside it? The tasks are queued and executed later by an agent, so it's vital that the task is not auto-started.</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          var w1 = wait1();
          w1.Start();
          w1.Wait(); // This waits 110 seconds
  
          var w2 = wait2();
          w2.Start();
          w2.Wait(); // This returns immediately
      }
  
      public static Task wait1()
      {
          return new Task(() =&gt;
          {
              Console.WriteLine("Waiting...");
              Thread.Sleep(10000);
              Console.WriteLine("Done!");
          });
      }
  
      public static Task wait2()
      {
          return new Task(async () =&gt;
          {
              Console.WriteLine("Waiting...");
              await Task.Delay(10000);
              Console.WriteLine("Done!");
          });
      }
  }
  </code></pre>