title: Starting tasks returned by iterator methods in parallel
body: <p>First look at this simple method:</p>
  
  <pre><code>Public Iterator Function GetLongRunningTasks(count As Long) As IEnumerable(Of Task)
      For i = 1 To count
          Yield Task.Delay(3000)
      Next
  End Function
  </code></pre>
  
  <p>This method returns the specified amount of tasks, each one of which completes 3 seconds after it starts. Let's call it a simulation of a network API call on an extremely bad network connection (not that it matters).</p>
  
  <p>My problem is that a simple iteration will start the tasks one at a time, so the 3000ms delay happens in between each iteration.</p>
  
  <pre><code>    For Each t In GetLongRunningTasks(50)
          Await t
      Next ' this takes ~150 seconds to complete (50x3000ms)
  </code></pre>
  
  <p>What I want to do is start all 50 tasks at once, then afterwards enter the foreach-loop. Preferably sticking to the example above, what is the proper way to do this?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As suggested by Stephen, one solution is to iterate over <code>GetLongRunningTasks(50).ToList()</code>. Maybe it's just me but I don't think it's at all obvious why ToList is used when I read over the code.</p>
  
  <p>I wonder if the following snippet is exactly the same?</p>
  
  <pre><code>    Dim tasks As New List(Of Task)
      tasks.AddRange(GetLongRunningTasks(50))
  
      For Each t In tasks
          Await t
      Next
  </code></pre>