【发布时间】:2013-03-09 18:01:07
【问题描述】:
编辑
这个问题的答案有帮助谢谢我的帮助:)但我最终使用了:http://code.msdn.microsoft.com/windowsdesktop/Handling-Unhandled-47492d0b#content
原问题:
我想在我的应用程序崩溃时显示错误消息。
目前我有:
App.xaml:
<Application x:Class="WpfApplication5.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DispatcherUnhandledException="App_DispatcherUnhandledException" // <----------------
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml 的代码隐藏:
namespace WpfApplication5
{
public partial class App : Application
{
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show("Caught Unhandled Exception!");
}
}
}
当错误发生在主线程上时,该解决方案效果很好。现在我的问题是我如何才能捕捉到另一个线程上发生的错误?
换句话说:当我按下这个按钮时,我能够捕捉到异常:(App_DispatcherUnhandledException 被调用!)
private void button1_Click(object sender, RoutedEventArgs e)
{
int.Parse("lkjdsf");
}
但我无法捕捉到这个异常:
private void button1_Click(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() =>
{
int.Parse("lkjdsf");
});
}
无论是否发生在主线程上,我如何才能捕获所有异常?
【问题讨论】:
标签: c# .net wpf exception unhandled-exception