title: Maximum number of Threads available to Tasks
body: <p>I'm Trying to get my head around the async-await functionality within C#. I've written the below code to run several tasks asynchronously - currently all they do is raise an event after a certain amount of time.  </p>
  
  <pre><code>public class Program
  {
      public static Stopwatch Watch = new Stopwatch();
  
      public static void Main(string[] args)
      {
          AsyncClass asyncClass = new AsyncClass();
          asyncClass.WaitSecondsAsyncCompleted += asyncClass_WaitSecondsAsyncCompleted;
          List&lt;Task&gt; tasks = new List&lt;Task&gt;();
          Watch.Start();
          for (int i = 1; i &lt; 6; i++)
          {
              tasks.Add(asyncClass.WaitSecondsAsync(i, Watch));
          }
          Task.WaitAll(tasks.ToArray());
          Console.ReadLine();
      }
  
      private static void asyncClass_WaitSecondsAsyncCompleted(int i)
      {
          Console.WriteLine("{1} : Async Method Called: waited for {0} seconds", i, Watch.ElapsedMilliseconds);
      }
  }
  
  public class AsyncClass
  {
      public event Action&lt;int&gt; WaitSecondsAsyncCompleted;
  
      public async Task WaitSecondsAsync(int x, Stopwatch watch)
      {
          await Task.Run(() =&gt;
          {   
              Thread.Sleep(x * 500); 
          });
  
          if (WaitSecondsAsyncCompleted != null)
          {
              WaitSecondsAsyncCompleted(x);
          }
      }
  }
  </code></pre>
  
  <p>I'd expect a task to be completed roughly once every half a second - however this is not quite what I see.  Instead the first four tasks complete on time but the final task has an extra half second delay:
  <a href="https://i.stack.imgur.com/nj3UX.png" rel="noreferrer"><img src="https://i.stack.imgur.com/nj3UX.png" alt="output of above code"></a></p>
  
  <p>This seems very strange - and the only thing I can think of is that there is a limit on the number of threads that are available to a task and that this is limit is very small and so the fifth task is having to wait for the first task to complete before it can start.  </p>
  
  <p>I added some extra output and increased the number of tasks to try and gain more information but I can make little sense of it - the output seems to be deterministic, some threads are reused, but also new ones are used.  The delay on tasks being completed also seems to continue to grow (for instance for Task 10 I'd expect it to complete after 5 seconds, instead it stops after 8 seconds).  I've attached the output below.</p>
  
  <p>What I'd like to know:</p>
  
  <ul>
  <li>Does anyone know what's going on in this particular example?</li>
  <li>Is the limit on threads available small enough to have an effect here?</li>
  <li>I presume asynchronous tasks are not guaranteed to start immediately, but there appears to be some other deterministic process going on here, which I hadn't expected.  Does anyone know what that is?</li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/S44DT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/S44DT.png" alt="output with extra debug info"></a></p>
  
  <hr>
  
  <h2>Edit</h2>
  
  <p>Note that this question does not ask about the maximum number of tasks that can be run (<a href="https://stackoverflow.com/questions/4740495/max-tasks-in-tpl">Max tasks in TPL?</a>) but rather how an effect can be seen when running as few as 5 tasks.  I was under the impression that default threadPool contained many more threads than this.</p>