捕获未处理从任何线程异常 [英] Catch unhandled exceptions from any thread

查看:229
本文介绍了捕获未处理从任何线程异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题的答案在哪里有用的感谢我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?

推荐答案

您可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx">AppDomain.UnhandledExceptionHandler处理未捕获的异常。

You can use AppDomain.UnhandledExceptionHandler to handle uncaught exception.

这篇关于捕获未处理从任何线程异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆