Created
March 15, 2011 22:46
-
-
Save kenegozi/871680 to your computer and use it in GitHub Desktop.
Demonstrating the use of WebClient.DownloadStringAsync to parallelize http requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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