title: What is going on with Task.Delay().Wait()?
body: <p>I'm confused why <code>Task.Delay().Wait()</code> takes <strong>4x more time</strong>, then <code>Thread.Sleep()</code>?  </p>
  
  <p>E.g. <strong>task-00</strong> was running on <strong>only thread 9</strong> and took <strong>2193ms</strong>?
  I'm aware, that sync wait is bad in tasks, because the whole thread being blocked. It is just for test.</p>
  
  <p>Simple test in console application:  </p>
  
  <pre><code>bool flag = true;
  var sw = Stopwatch.StartNew();
  for (int i = 0; i &lt; 10; i++)
  {
      var cntr = i;
      {
          var start = sw.ElapsedMilliseconds;
          var wait = flag ? 100 : 300;
          flag = !flag;
  
          Task.Run(() =&gt;
          {
              Console.WriteLine($"task-{cntr.ToString("00")} \t ThrID: {Thread.CurrentThread.ManagedThreadId.ToString("00")},\t Wait={wait}ms, \t START: {start}ms");                     
              //Thread.Sleep(wait);
              Task.Delay(wait).Wait();
              Console.WriteLine($"task-{cntr.ToString("00")} \t ThrID: {Thread.CurrentThread.ManagedThreadId.ToString("00")},\t Wait={wait}ms, \t END: {sw.ElapsedMilliseconds}ms");
              ;
          });
      }
  }
  Console.ReadKey();
  return;
  </code></pre>
  
  <p><strong>With <code>Task.Delay().Wait()</code>:</strong><br>
  task-03 ThrID: 05, Wait=300ms, START: 184ms<br>
  task-04 ThrID: 07, Wait=100ms, START: 184ms<br>
  task-00 ThrID: 09, Wait=100ms, START: 0ms<br>
  task-06 ThrID: 04, Wait=100ms, START: 185ms<br>
  task-01 ThrID: 08, Wait=300ms, START: 183ms<br>
  task-05 ThrID: 03, Wait=300ms, START: 185ms<br>
  task-02 ThrID: 06, Wait=100ms, START: 184ms<br>
  task-07 ThrID: 10, Wait=300ms, START: 209ms<br>
  task-07 ThrID: 10, Wait=300ms, END: 1189ms<br>
  task-08 ThrID: 12, Wait=100ms, START: 226ms<br>
  task-09 ThrID: 10, Wait=300ms, START: 226ms<br>
  task-09 ThrID: 10, Wait=300ms, END: 2192ms<br>
  task-06 ThrID: 04, Wait=100ms, END: 2193ms<br>
  task-08 ThrID: 12, Wait=100ms, END: 2194ms<br>
  task-05 ThrID: 03, Wait=300ms, END: 2193ms<br>
  task-03 ThrID: 05, Wait=300ms, END: 2193ms<br>
  task-00 ThrID: 09, Wait=100ms, END: 2193ms<br>
  task-02 ThrID: 06, Wait=100ms, END: 2193ms<br>
  task-04 ThrID: 07, Wait=100ms, END: 2193ms<br>
  task-01 ThrID: 08, Wait=300ms, END: 2193ms  </p>
  
  <p><strong>With <code>Thread.Sleep()</code>:</strong><br>
  task-00 ThrID: 03, Wait=100ms, START: 0ms<br>
  task-03 ThrID: 09, Wait=300ms, START: 179ms<br>
  task-02 ThrID: 06, Wait=100ms, START: 178ms<br>
  task-04 ThrID: 08, Wait=100ms, START: 179ms<br>
  task-05 ThrID: 04, Wait=300ms, START: 179ms<br>
  task-06 ThrID: 07, Wait=100ms, START: 184ms<br>
  task-01 ThrID: 05, Wait=300ms, START: 178ms<br>
  task-07 ThrID: 10, Wait=300ms, START: 184ms<br>
  task-00 ThrID: 03, Wait=100ms, END: 284ms<br>
  task-08 ThrID: 03, Wait=100ms, START: 184ms<br>
  task-02 ThrID: 06, Wait=100ms, END: 285ms<br>
  task-09 ThrID: 06, Wait=300ms, START: 184ms<br>
  task-04 ThrID: 08, Wait=100ms, END: 286ms<br>
  task-06 ThrID: 07, Wait=100ms, END: 293ms<br>
  task-08 ThrID: 03, Wait=100ms, END: 385ms<br>
  task-03 ThrID: 09, Wait=300ms, END: 485ms<br>
  task-05 ThrID: 04, Wait=300ms, END: 486ms<br>
  task-01 ThrID: 05, Wait=300ms, END: 493ms<br>
  task-07 ThrID: 10, Wait=300ms, END: 494ms<br>
  task-09 ThrID: 06, Wait=300ms, END: 586ms  </p>
  
  <p><strong>Edit:</strong><br>
  With <code>async</code> lambda and <code>await</code> <code>Task.Delay()</code> is as fast as <code>Thread.Sleep()</code>, may be also faster (511ms).<br>
  <strong>Edit 2:</strong><br>
  With <code>ThreadPool.SetMinThreads(16, 16);</code> <code>Task.Delay().Wait()</code> works as fast as <code>Thread.Sleep</code> for 10 iteration in the loop. With more iterations it's slower again. It's also interesting, that if without adjusting I increase the number of iterations for <code>Thread.Sleep</code> to <strong>30</strong>, it's still faster, then <strong>10</strong> iteration with <code>Task.Delay().Wait()</code><br>
  <strong>Edit 3:</strong><br>
  The overloading <code>Task.Delay(wait).Wait(wait)</code> works as fast as <code>Thread.Sleep()</code></p>