I am using delegate to invoke asyncronous call of the function. Sometimes the function can throw exception.
The code is like the following:
RecalculateDelegate dlgt = new RecalculateDelegate(CallbackWithException);
IAsyncResult asyncResult = dlgt.BeginInvoke(params, null, asyncState);
try
{
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.