Im trying to download strings (xml) from multiple sources asynchronously and wait for all to complete.
But when using Task.Factory.StartNew
to create multiple tasks, they all seem to download synchronously, one after another. All strings combined should weight aprox. 200kb so it shouldnt be a problem for my internet connection that can download 640kb per second.
Here is the code I'm using:
var taskList = new List<Task>();
foreach (var feed in feeds)
taskList.Add(Task.Factory.StartNew(() => DownloadFeed(feed)));
Task.WaitAll(taskList.ToArray());
Method that downloads:
void DownloadFeed(string url)
{
using (Webclient client = new WebClient())
concurrentBag.Add(client.DownloadString(url));
}
WebClient's async methods do work, they download asynchronously reducing the total downloading time to just a fraction. And while that should be good enough for my project, I'm still curious what is wrong with my code that uses Tasks?