1

I want to test my couple of webservices. How to send httpWebRequests parallely?

1

2 Answers 2

3

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());
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.