views:

1424

answers:

2

I am currently using the following code to create a web request:

Dim myRequest As WebRequest = WebRequest.Create("http://foo.com/bar")
Dim myResponse As WebResponse = myRequest.GetResponse()

The problem is that this "locks" up the program until the request is completed (and program will hang if the request never completes). How do you change something like this to execute asynchronously so that other tasks can be completed while the web request completes?

A: 

myRequest.BeginGetResponse()

You'll also need to call EndGetReponse() when the request is finished (determined via WaitHandle, callback, or polling).

Joel Coehoorn
+3  A: 

You'll use BeginGetResponse to add a AsyncCallback, which basically points to some other method in your code that will be called when the WebRequest returns. There is a good sample here.

http://www.sitepoint.com/forums/showpost.php?p=3753215

Ryan Farley