I have a code below to get a response from HTTP GET
:
private void ResponseReady(IAsyncResult aResult)
{
HttpWebRequest request = aResult.AsyncState as HttpWebRequest;
try
{
this.Dispatcher.BeginInvoke(delegate()
{
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(aResult);
The problem when there is no connection, it will stop at the response
row. It doesn't catch the exception. Is it because of the Dispatcher.Invoke
?
Your exception isn't caught because the call to BeginInvoke does not execute the code in your delegate, it queues it to be executed on a ThreadPool Thread. When your exception does occur there is no exception handling in place. Did you mean to use Invoke or BeginInvoke here? Either way putting the exception handling in the delegate should take care of your problems.
The code inside BeginInvoke delegate is executed in a different thread, you need to create a separate try/catch there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With