いくつかのスレッドをスピンオフするWPFアプリケーションがあります。App.xaml.csでDispatcherUnhandledExceptionイベントハンドラーを定義しました。このハンドラーは詳細なエラーメッセージを表示し、UIスレッドで例外が発生するたびにこのハンドラーが呼び出されます。問題は子スレッドにあります。未処理の例外が処理されることはありません。どうすればよいですか?
サンプルコード:
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show("detailed error message");
}
private void Application_Startup(object sender, StartupEventArgs e)
{
//...
//If an Exception is thrown here, it is handled
//...
Thread[] threads = new Thread[numThreads];
for(int i = 0; i < numThreads; i++)
{
threads[i] = new Thread(doWork);
threads[i].Start();
}
}
private void doWork()
{
//...
//Exception thrown here and is NOT handled
//...
}
編集:未処理の例外が発生したら、スタックトレースを含むエラーメッセージを表示して、アプリケーションを終了します。