title: Asynchronously invoking a parallel for loop
body: <p>I would like to do something like this:</p>
  
  <pre><code>public async Task MyMethod()
  {
      // Do some preparation
  
      await Parallel.ForEachAsync(0, count, i =&gt; { // Do some work //});
  
      // do some finalization
  }
  </code></pre>
  
  <p>However, I did not find an elegant way of doing so. I thought of two ways, but they are sub-optimal:</p>
  
  <ol>
  <li>The only thing I thought about is manually partitioning the range, creating tasks, and then using Task.WhenAll. </li>
  <li>Using the following code <code>Task.Factory.StartNew(() =&gt; Parallel.For(...));</code>.<br>
  The problem is that it "wastes" a thread on the asynchronous task.</li>
  <li>Using TPL Dataflow's ActionBlock, and posting the integers one by one. The drawback is that it does not partition the range in a smart way like Parallel.For does, and works on each iteration one by one.</li>
  <li>Manually using a Partitioner with Partitioner.Create, but it is less elegant. I want the framework to do intelligent partitioning for me.</li>
  </ol>