Catching Exceptions across threads (RE: HttpWebRequest)

  • Thread starter Thread starter Danny
  • Start date Start date
D

Danny

Hi.

I have an app that is using HttpWebRequest to get data from Web sites.
Occasionally, the CLR throws an IO exception in System.dll that reads
"cannot read from the transport connection." This exception is thrown on
another thread (I am guessing a system thread pool thread). As such, I
cannot catch the exception in the thread from which I invoked the request!

This is very frustrating because ideally, I would like to ignore the
exception and keep fetching more documents from other URLs.

Any suggestions on how to handle this dilemma? I dont control the code from
which the exception gets thrown so I dont have any recourse from my main
thread.

Thanks!

-Danny-
 
What happens when the exception is thrown? Does a message box pop up, the
app terminate, or do you just see the notification in the output window of
the debugger? If the exception is in a threadpool thread then the exception
should be swallowed by the system.

Have you tried adding an AppDomain.UnhandledException handler?
 
Thanks Dave!

A message box pops up indicating that an unhandled IO exception has
occurred. It occurs on the threadpool thread and the exception is not
swallowed by the system. That indeed is the problem I am facing - I cant
catch it from my thread but the system isnt catching it on its thread
either.

Here is the call stack:
system.dll!System.Net.Connection.ReadCallback(System.IAsyncResult
asyncResult = {System.Net.Sockets.OverlappedAsyncResult}) + 0x363 bytes

system.dll!System.Net.LazyAsyncResult.InvokeCallback() + 0x25
bytes

system.dll!System.Net.LazyAsyncResult.InvokeCallback(bool
completedSynchronously = false, System.Object result = {4096}) + 0x27 bytes


system.dll!System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback(u
int errorCode = 0, uint numBytes = 4096, System.Threading.NativeOverlapped*
nativeOverlapped = 121733296) + 0xe4 bytes

Also, no, I havent added an AppDomain.UnhandledException handler. Can I do
this even though I am running within the context of ASP.NET? Wouldnt this
affect other running apps? My app is a Web service.

Thanks!

-Danny-
 
Hi,

Consume that exception, try to catch that exception using
(System.Threading.ThreadAbortException) in both the
places.

Try this and let me know it worked out for you.

Regards
Mahesh ChandraMouli
Microsoft .NET MVP|MCAD
 
Hi Dave.

I added an AppDomain.UnhandledException handler to my code and this
successfully consumed the exception. However, the CLR (via ASP.NET) does not
resume my app from where it left off (which is what I want). Is there a way
to indicate that the calling thread should resume as normal (in this case,
this is a thread from the System Thread Pool).

Thanks in advance!

-Danny-
 
Thanks Mahesh.

I already have try-catch blocks around all my calls to HttpWebRequest (I
consume all exceptions including ThreadAbortException) but they dont help
because the exception occurs on a thread-pool thread. I also tried trapping
the exception via AppDomain.UnhandledException handler but that resulted in
my app being left in an unstable state (ASP.NET appeared to stop after
that).

I notice from searching on the Web that others have also seen this issue.
Any help would be much appreciated.

Thanks.

-Danny-
 
The handler gets invoked if the exception was not handled, and by then it's
too late for you to do anything about it other then to take note of it.
There may be some other way you can avoid this (intercept the messages
before it gets to the handler?) but it's beyond my level of knowledge. You
might try asking this same question on one of the ASP news groups.
 
Back
Top