Difference Between BeginGetResponse and GetResponse
-
Hello Does anyone have already experienced the BeginGetResponse illutrated in the Microsoft example ? http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse.aspx[^] My need will be to make an hrttprequest in asynchronous mode. To not let the user wait for a response that can be processed in backround. First I was thinking to use a BackroundWorker, then I see that begingetresponse example But it react exacly as the BeginResponse : when I step over it wait a few seconds to get the response. So maybe I misunderstand something but I don't see any advantage of this method ?? Thank for your help
-
Hello Does anyone have already experienced the BeginGetResponse illutrated in the Microsoft example ? http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse.aspx[^] My need will be to make an hrttprequest in asynchronous mode. To not let the user wait for a response that can be processed in backround. First I was thinking to use a BackroundWorker, then I see that begingetresponse example But it react exacly as the BeginResponse : when I step over it wait a few seconds to get the response. So maybe I misunderstand something but I don't see any advantage of this method ?? Thank for your help
When you run it, is it timing out? Or is it receiving a response?
The difficult we do right away... ...the impossible takes slightly longer.
-
Hello Does anyone have already experienced the BeginGetResponse illutrated in the Microsoft example ? http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse.aspx[^] My need will be to make an hrttprequest in asynchronous mode. To not let the user wait for a response that can be processed in backround. First I was thinking to use a BackroundWorker, then I see that begingetresponse example But it react exacly as the BeginResponse : when I step over it wait a few seconds to get the response. So maybe I misunderstand something but I don't see any advantage of this method ?? Thank for your help
BeginGetResponse will execute the method in the AsyncCallBack asynchronously. If you do like in the MSDN example then it will wait for that asynchronous operation to finish this is the code that will cause it to wait and also implements a timeout method so if the asynchronous operation takes to long it will end the asynchronous operation.
// this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
ThreadPool.RegisterWaitForSingleObject (result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myHttpWebRequest, DefaultTimeout, true);// The response came in the allowed time. The work processing will happen in the // callback function. allDone.WaitOne();
-
BeginGetResponse will execute the method in the AsyncCallBack asynchronously. If you do like in the MSDN example then it will wait for that asynchronous operation to finish this is the code that will cause it to wait and also implements a timeout method so if the asynchronous operation takes to long it will end the asynchronous operation.
// this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
ThreadPool.RegisterWaitForSingleObject (result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myHttpWebRequest, DefaultTimeout, true);// The response came in the allowed time. The work processing will happen in the // callback function. allDone.WaitOne();
Thank for the answer But it seems that the BeginGetResponse itself takes long to come back // Start the asynchronous request. IAsyncResult result= (IAsyncResult) myHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback),myRequestState); I test it a few times yesterday But today I try again and now it return immediately ! Even if I cut the network connexion ! I probably miss something !:confused: Some told me that I have to wrap this code example in a dedicated thread ! But if so I dond see the advantage of using the BeginGetResponse
-
Thank for the answer But it seems that the BeginGetResponse itself takes long to come back // Start the asynchronous request. IAsyncResult result= (IAsyncResult) myHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback),myRequestState); I test it a few times yesterday But today I try again and now it return immediately ! Even if I cut the network connexion ! I probably miss something !:confused: Some told me that I have to wrap this code example in a dedicated thread ! But if so I dond see the advantage of using the BeginGetResponse
I'm thinking you must have missed something initially, BeginGetResponse should return immedialtely. The only reason I would see to run that in a dedicated thread is if you want to worry about a timeout or want to ensure the asynchronous method completes like the example, as the method will block waiting for the ManualResetEvent to be set. Using the ManualResetEvent is for situations where you can't process the next step until that process has completed.
-
I'm thinking you must have missed something initially, BeginGetResponse should return immedialtely. The only reason I would see to run that in a dedicated thread is if you want to worry about a timeout or want to ensure the asynchronous method completes like the example, as the method will block waiting for the ManualResetEvent to be set. Using the ManualResetEvent is for situations where you can't process the next step until that process has completed.
-
Thank you Trak4Net Ok ... maybe I was too tired yesterday I will try again tonight But indeed I'm expecting that BeginGetResponse return immediately whatever the connexion state !
Some more investigation turns out this is a known issue. Apparently it blocks during the name resolution so does not return immediately as you would think. Sorry, I should have looked into it a little more before replying. http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/2cb74a7e-6e8f-4d05-b86a-2401df5d2ed3/[^]