0

I need to do several HttpWebRequest requests in a single moment, but when I make the request several times will cost a lot of time. I plan on doing each request in a separate thread and then get all the returns and put it all together when all calls are completed is it possible?

Cœur
  • 34,719
  • 24
  • 185
  • 251
mcamara
  • 711
  • 4
  • 15
  • 25

2 Answers2

2

Absolutely.

Take a look at the msdn docs on HttpWebRequest.BeginGetResponse Method

I couldn't write a better example than in the docs.

hometoast
  • 11,293
  • 4
  • 39
  • 57
  • In this case, for each request made, will be called in a different thread? – mcamara Sep 28 '11 at 15:00
  • Right. You can kick off a bunch of threads, keep an array of ManualResetEvents, and then use WaitAll() to wait for all the web requests to finish. – hometoast Sep 28 '11 at 16:00
1

I don't know much about Threading, but as they run asynchronously, getting a return value isn't an option. However, this thread : http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/c43c0cce-491a-4ddb-9012-69929fab8e5e/ does point towards the ThreadPool class.

pikzen
  • 1,407
  • 1
  • 10
  • 18