Managing Unhandled Exceptions on Secondary Worker Threads Sample
This sample demonstrates how to manage unhandled exceptions that are thrown on secondary worker threads.
This sample demonstrates a specific feature of the Windows Presentation Foundation and, consequently, does not follow application development best practices. For comprehensive coverage of Windows Presentation Foundation (WPF) and Microsoft .NET Framework application development best practices, refer to the following as appropriate:
Accessibility - Accessibility Best Practices
Localization - WPF Globalization and Localization Overview
Performance - Optimizing WPF Application Performance
Security - Windows Presentation Foundation Security
Install the Windows Software Development Kit (SDK) and open its build environment command window. On the Start menu, point to All Programs, Microsoft Windows SDK, and then click CMD Shell.
Download the sample, usually from the software development kit (SDK) documentation, to your hard disk drive.
To build the sample from the build environment command window, go to the source directory of the sample. At the command prompt, type MSBUILD.
To build the sample in Microsoft Visual Studio, load the sample solution or project file and then press CTRL+SHIFT+B.
Standalone or browser-hosted WPF applications use the Application class to detect unhandled exceptions (see DispatcherUnhandledException). However, Application can only detect unhandled exceptions that are thrown on the same thread that the Application class is running. Usually, an application will have one main user interface (UI) thread, so the unhandled exception detection behavior of the Application class is adequate. However, unhandled exceptions that are thrown on secondary threads are not automatically detected by the Application class on the main UI thread.
This sample demonstrates how to pass unhandled exceptions from secondary worker threads back to the main UI thread where they can be detected by the Application object. This is achieved by detecting unhandled exceptions on the secondary worker thread and using the Dispatcher to forward them to the main UI thread. On the main UI thread, they are rethrown so that the Application object can detect them.
A secondary worker thread is a secondary thread that does not display UI.