The details of this problem is introduced in “Catch DispatcherUnhandledException from other Thread” here:
http://forums.microsoft.com/msdn/showpost.aspx?postid=1164067&siteid=1
Chango V. of Microsoft answers this issue with this pattern:
(i) Make the Dispatcher object of the main thread available to your worker thread. (You could either pass it as a parameter to the thread start method or just use Application.Current.Dispatcher.)
(ii) Use a try-catch-finally in the worker thread. (In general, the catch block should immediately rethrow critical exceptions: OutOfMemoryException, SecurityException, SEHException, maybe also NullReferenceException.)
(iii) The [worker-thread] catch block calls mainDispatcher.Invoke(DispatcherPriority.Send, ExceptionCallback, exception).
(iv) ExceptionCallback() throws the exception object passed to it. This happens on the main thread, so handlers added to Application.DispatcherUnhandledException will be invoked and can “handle” the exception.
(v) If your worker thread resumes execution, the exception was “handled.”
The following is a worker-thread catch block following this pattern in a WPF application:
catch ( Exception ex )
{
Dispatcher d = ClientApplication.Current.Dispatcher;
ClientApplication.WorkerThreadExceptionCallback delg =
ClientApplication.WorkerThreadException;
d.Invoke( DispatcherPriority.Send, delg, ex );
}
The main thread defines:
internal static void
WorkerThreadException( Exception WorkerThreadException )
{
throw WorkerThreadException as Exception;
}
internal delegate void
WorkerThreadExceptionCallback( Exception WorkerThreadException );