cdub
cdub

Reputation: 25701

Page Lifecycle and Asynchronous Requests in C# asp.net web forms

If I send an HttpWebRequest and send it out with BeginGetResponse with a callback, my callback never gets hit. THe page finishes before the response is given.

How do I grab the response?

I have tried setting a timer:

  ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), null, DefaultTimeout, true);

but again if DefaultTimeout (in milliseonds) is set to 1 millisecond, it reaches the callback. But if it is set to 30 seconds, then the callback never gets fired.

How do I access the request/result again?

Upvotes: 0

Views: 428

Answers (1)

Brandon Montgomery
Brandon Montgomery

Reputation: 6986

If you want to wait for the web request to complete before allowing your code to continue executing, you'll need to use the synchronous GetResponse method instead of the asynchronous BeginGetResponse method. This will block the current thread until the request completes.

Upvotes: 2

Related Questions