Why exceptions are not propagated by WPF Dispatcher.Invoke? Why exceptions are not propagated by WPF Dispatcher.Invoke? wpf wpf

Why exceptions are not propagated by WPF Dispatcher.Invoke?


UPDATED: To observe the exception in the other thread, you want to use a Task, queue it to the Dispatcher thread (using TaskScheduler.FromCurrentSynchronizationContext), and wait on it, as such:

var ui = TaskScheduler.FromCurrentSynchronizationContext();Action doit = () => {     var error = Task.Factory.StartNew(        () => { throw new InvalidOperationException("test"); },        CancellationToken.None,        TaskCreationOptions.None,        ui);     try {         error.Wait();     } catch (Exception ex) {         System.Diagnostics.Trace.WriteLine(ex);     } }; doit.BeginInvoke(null, null); 

UPDATED (again): Since your goal is a reusable component, I do recommend moving to a Task-based interface or something else based on SynchronizationContext such as the event-based asynchronous pattern, instead of basing the component on Dispatcher or ISynchronizeInvoke.

Dispatcher-based components only work on WPF/Silverlight; ISynchronizeInvoke-based components only work on Windows Forms. SynchronizationContext-based components will work with WPF or Windows Forms transparently, and (with a bit more work) ASP.NET, console apps, windows services, etc.

The event-based asynchronous pattern is the old recommended way of writing SynchronizationContext-based components; it's still around for .NET 3.5-era code. If you're on .NET 4, though, the task parallel library is much more flexible, clean, and powerful. The TaskScheduler.FromCurrentSynchronizationContext uses SynchronizationContext underneath, and is the New Way to write reusable components that need this kind of synchronization.