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?
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.
精彩评论