Skip to content

Instantly share code, notes, and snippets.

@kenegozi
Created March 15, 2011 22:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kenegozi/871680 to your computer and use it in GitHub Desktop.
Save kenegozi/871680 to your computer and use it in GitHub Desktop.
Demonstrating the use of WebClient.DownloadStringAsync to parallelize http requests
class ParallelHttpRequests
{
static void Main()
{
var count = 300;
var root = "http://files.kenegozi.com/temp/";
ServicePointManager.DefaultConnectionLimit = 1000;
var watch = Stopwatch.StartNew();
for (var i = 0; i < count; ++i)
{
var client = new WebClient();
var url = string.Format("{0}file{1:0000}.txt", root, i);
client.DownloadString(url);
}
Console.WriteLine("for loop: " + watch.Elapsed);
watch = Stopwatch.StartNew();
Parallel.For(0, count, i =>
{
var client = new WebClient();
var url = string.Format("{0}file{1:0000}.txt", root, i);
client.DownloadString(url);
});
Console.WriteLine("parallel: " + watch.Elapsed);
watch = Stopwatch.StartNew();
var completed = new CountdownEvent(count);
for (var i = 0; i < count; ++i)
{
var client = new WebClient();
client.DownloadStringCompleted += (s, e) => completed.Signal();
var url = string.Format("{0}file{1:0000}.txt", root, i);
client.DownloadStringAsync(new Uri(url));
}
completed.Wait();
Console.WriteLine("async: " + watch.Elapsed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment