I want to test my couple of webservices. How to send httpWebRequests parallely?
2 Answers
Did you try to use the Task Parallel library. You can find more information here.
For example you can call the Invoke method to perform a couple of delegates in parallel:
Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork());
Comments
Try this:
new List<string>
{
"http://www.stackoverflow.com",
"http://www.google.com"
}
.AsParallel().ForAll(x =>
{
var client = new WebClient();
client.DownloadStringAsync(new Uri(x));
client.DownloadStringCompleted +=
(o, e) =>
{
var result = e.Result; // html will be here
Console.WriteLine("Completed");
};
});
Or this:
Parallel.ForEach(new List<string>
{
"http://www.stackoverflow.com",
"http://www.google.com"
}, x =>
{
var client = new WebClient();
client.DownloadStringAsync(new Uri(x));
client.DownloadStringCompleted +=
(o, e) =>
{
var result = e.Result; // html will be here
Console.WriteLine("Completed");
};
}
For more information read Parallel Programming