Jan 04, 2010 04:01 PM|atconway|LINK
I think the issue is arising because you are calling the 'EndInvoke' method from the delegate method rather than from the original Page_Load() method where you invoked the async delegate. It is correct in that you do need to call the EndInvoke in order
to catch exceptions, or otherwise they would be discarded, but I think it may need to be called in Page_Load().
The following page has a sample and situation similar to yours; they poll while the main thread works, and then calls the 'EndInvoke' after the main thread has completed processing. You may want to test it in this manner as well to see if you can get the
Exception Handling Thrown from an Asynchronous Delegate:
Here is an excerpt from the above that details the situation:
"If the code in the PollAsyncDelegate method did not contain a call to the delegate's EndInvoke method, the exception thrown in Method1 either would simply be discarded and never caught or, if the application had the top, level exception handlers wired
up (Recipes 7.10 and 7.20) it would be caught. If EndInvoke is called, then this exception would occur when EndInvoke is called and could be caught there. This behavior is by design; for all unhandled exceptions that occur within the thread, the thread immediately
returns to the thread pool and the exception is lost.
If a method that was called asyncresultshronously through a delegate throws an exception, the only way to trap that exception is to include a call to the delegate's EndInvoke method and wrap this call in an exception handler. The EndInvoke method must
be called to retrieve the results of the asynchronous delegate; in fact, the EndInvoke method must be called even if there are no results. These results can be obtained through a return value or any ref or out parameters of the delegate."
There is also a good example of calling the EndInvoke method properly if you scroll to the 2nd code example in the following MSDN link:
Calling Synchronous Methods Asynchronously (Waiting for an Asynchronous Call with EndInvoke):
Hope this helps!