捕获未处理从任何线程异常
在这个问题的答案在哪里有用的感谢我AP preciate帮助:)但我最终使用:的http://$c$c.msdn.microsoft.com/windowsdesktop/Handling-Unhandled-47492d0b#content
The answers to this question where helpful thanks I appreciate the help :) but I ended up using: http://code.msdn.microsoft.com/windowsdesktop/Handling-Unhandled-47492d0b#content
我要显示一个错误消息时,我的应用程序崩溃。
I want to show a error message when my application crashes.
目前我有:
的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>
code-背后的的App.xaml
:
namespace WpfApplication5
{
public partial class App : Application
{
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show("Caught Unhandled Exception!");
}
}
}
这是解决方案的伟大工程时,在主线程中出现错误。现在,我的问题是我怎么样才能赶上这种情况发生在不同的线程也错误?
That solution works great when the error occurs on the main thread. Now my problem is how will I be able to catch errors that happen on a different thread also?
在换句话说:当我preSS这个按钮,我能捕捉到了异常:(! App_DispatcherUnhandledException
被调用)
In other words: when I press this button I am able to catch the exception: (App_DispatcherUnhandledException
gets called!)
private void button1_Click(object sender, RoutedEventArgs e)
{
int.Parse("lkjdsf");
}
但我不能够捕获这个异常:
But I am not able to catch this exception:
private void button1_Click(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() =>
{
int.Parse("lkjdsf");
});
}
我如何能捕获所有异常,不管它们发生在主线程或没有?
How will I be able to catch all exceptions regardless if they happen on the main thread or not?
您可以使用AppDomain.UnhandledExceptionHandler处理未捕获的异常。
You can use AppDomain.UnhandledExceptionHandler to handle uncaught exception.