title: Dynamically assigning tasks to a Task array for Task.Factory.ContinueWhenAll to process
body: <p>I've been playing around with the Samples for Parallel Programming with the .NET Framework located <a href="https://devblogs.microsoft.com/pfxteam/samples-for-parallel-programming-with-the-net-framework-4/" rel="nofollow noreferrer">here</a>, and learning a bunch of really great things.</p>
  <p>Specifically I'm trying to build a simple program that pulls in a list of rss feeds and then parses out specific information.</p>
  <p>In the solution, <code>AsyncDownload_CSharp</code>, it provides a number of examples of how to download asynchronously. I've got a pretty decent understanding of the following piece of code:</p>
  <pre><code>// Concurrent async requests
  Task.Factory.ContinueWhenAll(new []
  {
      Download(&quot;http://blogs.msdn.com/pfxteam&quot;),
      Download(&quot;http://blogs.msdn.com/nativeconcurrency&quot;),
      Download(&quot;http://exampleexampleexample.com&quot;), // will fail
      Download(&quot;http://msdn.com/concurrency&quot;),
      Download(&quot;http://bing.com&quot;)
  }, ConcurrentTasksCompleted).Wait();
  </code></pre>
  <p>The code above has the list of Tasks hard code and I've been trying to figure out how would I go about generating that list of Tasks dynamically where I assign the value of url at runtime either through a <code>foreach</code> loop or some other repeat loop.</p>
  <p>The code I came up with to build the list of Tasks dynamically and pass it to the <code>Task.Factory.ContinueWhenAll looks</code> like this:</p>
  <pre><code>private void btnFeeds_Click(object sender, EventArgs e)
  {
      string[] urls = new[] { &quot;http://blogs.msdn.com/pfxteam&quot;,
          &quot;http://msdn.com/concurrency&quot;, &quot;http://bing.com&quot; };
      List&lt;Task&lt;string&gt;&gt; tasks = new List&lt;Task&lt;string&gt;&gt;();
  
      foreach (string url in urls)
      {
          tasks.Add(Download(url));
      }
      
      Task.Factory.ContinueWhenAll(tasks, ConcurrentTasksCompleted).Wait();
  
  }
  </code></pre>
  <p>Unfortunately, when I step through the debugger not only do I see the Tasks list being built but as they're being added to the tasks list but I also see them being executed before being passed to the <code>Task.Factory.ContinueWhenAll</code>.</p>
  <p>I'm thinking I need to use a lambda expression, instead of that <code>foreach</code> loop, to pass each of these to the <code>Task.Factory.ContinueWhenAll</code>. Unfortunately, I'm having a hard time figuring out how the url for the task is applied using the lambda expression.</p>
  <p>I'm not sure if what I'm trying to do is even possible, so any suggestions, pointers, or help would be greatly appreciated.</p>