Manage unhandled exceptions that are thrown on secondary user interface (UI) threads.
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.App"
StartupUri="MainWindow.xaml"
DispatcherUnhandledException="App_DispatcherUnhandledException" />
//File:Window.xaml.cs
using System;
using System.Text;
using System.Windows;
using System.Windows.Threading;
namespace WpfApplication1
{
public partial class App : Application
{
void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}\n", e.Exception.InnerException.Message);
sb.AppendFormat("{0}\n", e.Exception.Message);
sb.AppendFormat("Exception handled on main UI thread {0}.", e.Dispatcher.Thread.ManagedThreadId);
MessageBox.Show(sb.ToString());
e.Handled = true;
}
}
}
Related examples in the same category