Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispatcher.Invoke and propagating errors

I have a splash screen for a WPF (using .net 4.5 and mvvmlight) that must perform various load operations in an async manner, showing progress and occasionally asking for user input.

When asking for input, I'll create forms/dialogs off the UI thread to call ShowDialog (with the splash screen as the parent) so that no cross-threading issues occur. This all works fine BUT if an error occurs when asking for input, the resulting exception is lost.

The examples below don't follow MVVM at all for simplicity.

Here is my app.cs, which set the UI dispatcher and is prepared to handle any unhandled dispatcher exceptions for error reporting:

public partial class App : Application
    {
        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
            System.Windows.Forms.MessageBox.Show("Exception Handled");
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            GalaSoft.MvvmLight.Threading.DispatcherHelper.Initialize();
        }
    }

And here is my (very simplified) startup/splash screen:

    private void Window_ContentRendered(object sender, EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("Starting long running process...");

            var t = System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                //some kind of threaded work which decided to ask for user input.
                    GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher.Invoke(() =>
                {
                    //Show form for user input, launched on UIDispatcher so that it's created on the UI thread for ShowDialog etc
                    throw new Exception("issue in capturing input");
                });
            });
        }

So I'm asking for user input through Invoke (because I want to wait for the answer) but even though I'm calling the work through the UIDispatcher, Application_DispatcherUnhandledException is never fired and the exception is lost. What am I missing? The example is using a Task for the threaded job but this also occurs when using BeginInvoke(). Surely the work (and resulting exception) should be occurring on the UIDispatcher?

UPDATE: Alternative demonstration (exception not handled) using BeginInvoke

private void Window_ContentRendered(object sender, EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("Starting long running process...");

            Action anon = () =>
                {
                    //some kind of threaded work which decided to ask for user input.
                        GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher.Invoke(() =>
                    {
                        //Show form for user input, launched on UIDispatcher so that it's created on the UI thread for ShowDialog etc
                        throw new Exception("issue in capturing input");
                    });
                };

            anon.BeginInvoke(RunCallback, null);
        }

        private void RunCallback(IAsyncResult result)
        {
            System.Windows.Forms.MessageBox.Show("Completed!");
        }
like image 219
MoSlo Avatar asked Jun 23 '16 12:06

MoSlo


1 Answers

Using Task

The exception is handled by the task, so DispatcherUnhandledException wouldn't fire. This is because you use the synchronous Dispatcher.Invoke method - which is almost always a bad practice; you're wasting time on a thread-pool thread waiting for the UI to perform some operation. You should prefer Dispatcher.BeginInvoke or (when using await) Dispatcher.InvokeAsync.

In addition, it may be a good idea to register for the TaskScheduler.UnobservedTaskException event, so that exceptions like these could be logged (this only happens after the Task is garbage collected).

Lastly, if you are able to use C# 5 or above, I strongly recommend taking a look at async/await. The above method could be rewritten as:

    private async void Window_ContentRendered(object sender, EventArgs e)
    {
        MessageBox.Show("Starting long running process...");

        await Task.Run(() =>
        {
            //some kind of threaded work
            throw new Exception("foo");
        });

        // code after the await will automatically be executed on the UI thread
        // the await will also propagate exceptions from within the task
        throw new Exception("issue in capturing input");
    }

Using Delegate.BeginInvoke

Here we execute an operation on the thread-pool as well, but the exception is handled by the "async result" object. I discourage you entirely from using this old threading model (APM).

Incidentally, you can get the exception to throw if you call the corresponding EndInvoke (which you should be doing anyway):

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        Action a = () => { Dispatcher.Invoke(() => { throw new Exception(); }); };
        a.BeginInvoke(Callback, a);
    }

    private void Callback(IAsyncResult ar)
    {
        ((Action)ar.AsyncState).EndInvoke(ar);
    }

But even then, DispatcherUnhandledException will not be called since the callback is executed on a thread-pool thread. So the process will simply crash.

Conclusion

Using a synchronous Dispatcher.Invoke will always propagate the exception to the caller. It is also very wasteful to use. If the caller is not a UI thread, the exception will never reach the dispatcher, and depending on the threading API used, it will either be swallowed or thrown and crash the process.

like image 183
Eli Arbel Avatar answered Nov 06 '22 04:11

Eli Arbel