title: How to correctly queue up tasks to run in C#
body: <p>I have an enumeration of items (<code>RunData.Demand</code>), each representing some work involving calling an API over HTTP.  It works great if I just <code>foreach</code> through it all and call the API during each iteration.  However, each iteration takes a second or two so I'd like to run 2-3 threads and divide up the work between them.  Here's what I'm doing:</p>
  
  <pre><code>ThreadPool.SetMaxThreads(2, 5); // Trying to limit the amount of threads
  var tasks = RunData.Demand
     .Select(service =&gt; Task.Run(async delegate
     {
        var availabilityResponse = await client.QueryAvailability(service);
        // Do some other stuff, not really important
     }));
  
  await Task.WhenAll(tasks);
  </code></pre>
  
  <p>The <code>client.QueryAvailability</code> call basically calls an API using the <code>HttpClient</code> class:</p>
  
  <pre><code>public async Task&lt;QueryAvailabilityResponse&gt; QueryAvailability(QueryAvailabilityMultidayRequest request)
  {
     var response = await client.PostAsJsonAsync("api/queryavailabilitymultiday", request);
  
     if (response.IsSuccessStatusCode)
     {
        return await response.Content.ReadAsAsync&lt;QueryAvailabilityResponse&gt;();
     }
  
     throw new HttpException((int) response.StatusCode, response.ReasonPhrase);
  }
  </code></pre>
  
  <p>This works great for a while, but eventually things start timing out.  If I set the HttpClient Timeout to an hour, then I start getting weird internal server errors.</p>
  
  <p>What I started doing was setting a Stopwatch within the <code>QueryAvailability</code> method to see what was going on.</p>
  
  <p>What's happening is all 1200 items in RunData.Demand are being created at once and all 1200 <code>await client.PostAsJsonAsync</code> methods are being called.  It appears it then uses the 2 threads to slowly check back on the tasks, so towards the end I have tasks that have been waiting for 9 or 10 minutes.</p>
  
  <p>Here's the behavior I would like:</p>
  
  <p>I'd like to create the 1,200 tasks, then run them 3-4 at a time as threads become available.  I do <em>not</em> want to queue up 1,200 HTTP calls immediately.</p>
  
  <p>Is there a good way to go about doing this?</p>