Geeks With Blogs

News My Blog has been MOVED to
Michael Freidgeim's OLD Blog My Blog has been MOVED to
I am using delegate to invoke asyncronous call of the function. Sometimes the function can throw exception.
As described in thread Re: exception handling with events , article "Handling Exceptions Thrown from an Asynchronous Delegate"(and in many other places)
"You should call EndInvoke in a try/catch block to catch any exceptions
your asynchronous method threw". I followed the same approach in my Asynchronous long-running tasks in ASP.NET application  
The code is like the following:
 RecalculateDelegate dlgt = new RecalculateDelegate(CallbackWithException);
IAsyncResult asyncResult = dlgt.BeginInvoke(params, null, asyncState); 
bool bRet=dlgt.EndInvoke(asyncResult);// 
  if (bRet == false
    Debug.Assert(false, "To Implement"); 
catch (Exception exc)
   EventSources.RaiseErrorMessage(EventSources.MyEventSource, message, ExceptionSeverity.Error, exc);
However when I tried to debug, VS showed me the message "An exception of type 'xxx' occurred in xxx.dll but was not handled in user code". After this it also not executing catch block, but hangs for a while and then timeouts in the client(the code is located in .Net Remoting server method.)

 In production (Release mode without any debugging) it seems  that catch block is invoked(I can see event log entries).

 The discussion Exceptions in asynchronous delegates  suggests to change VS setting to avoid the message. However I feel that more reliable approach will be catch (and log) all exceptions in the delegate function to ensure that they are not thrown asyncronously.

 Related posts:
Strange: calling EndInvoke from an AsyncCallback - why UnhandledException -too long and WinForms specific.

Posted on Wednesday, December 3, 2008 10:45 PM .Net Framework , Testing/Debugging/Logging | Back to top

Comments on this post: Avoid throwing exception from asynchronous delegate.

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Michael Freidgeim | Powered by: