WPF全局异常处理程序

本文翻译自:WPF global exception handler [duplicate]

This question already has an answer here: 这个问题已经在这里有了答案:

Sometimes, under not reproducible circumstances, my WPF application crashes without any message. 有时,在不可复制的情况下,我的WPF应用程序崩溃而没有任何消息。 The application simply close instantly. 该应用程序只是立即关闭。

Where is the best place to implement the global Try/Catch block. 哪里是实现全局Try / Catch块的最佳位置。 At least I have to implement a messagebox with: "Sorry for the inconvenience ..." 至少我必须用以下消息实现消息框:“抱歉给您带来的不便...”


#1楼

参考:https://stackoom.com/question/6B3y/WPF全局异常处理程序


#2楼

You can handle the AppDomain.UnhandledException event 您可以处理AppDomain.UnhandledException事件

EDIT: actually, this event is probably more adequate: Application.DispatcherUnhandledException 编辑:实际上,此事件可能更合适: Application.DispatcherUnhandledException


#3楼

You can trap unhandled exceptions at different levels: 您可以在不同级别捕获未处理的异常:

  1. AppDomain.CurrentDomain.UnhandledException From all threads in the AppDomain. AppDomain.CurrentDomain.UnhandledException来自AppDomain.CurrentDomain.UnhandledException所有线程。
  2. Dispatcher.UnhandledException From a single specific UI dispatcher thread. Dispatcher.UnhandledException来自单个特定的UI调度程序线程。
  3. Application.Current.DispatcherUnhandledException From the main UI dispatcher thread in your WPF application. Application.Current.DispatcherUnhandledException来自WPF应用程序中的 UI调度程序线程。
  4. TaskScheduler.UnobservedTaskException from within each AppDomain that uses a task scheduler for asynchronous operations. 每个使用任务计划程序进行异步操作的AppDomain中的TaskScheduler.UnobservedTaskException

You should consider what level you need to trap unhandled exceptions at. 您应该考虑需要在什么级别捕获未处理的异常。

Deciding between #2 and #3 depends upon whether you're using more than one WPF thread. 在#2和#3之间进行选择取决于您是否使用多个WPF线程。 This is quite an exotic situation and if you're unsure whether you are or not, then it's most likely that you're not. 这是一个非常特殊的情况,如果您不确定自己是否在,那么很可能你不是。


#4楼

为了补充托马斯的答案, Application类还具有您可以处理的DispatcherUnhandledException事件。


#5楼

In addition to the posts above: 除了以上职位:

Application.Current.DispatcherUnhandledException

will not catch exceptions that are thrown from another thread then the main thread. 不会捕获从其他线程(然后是主线程)引发的异常。 You have to handle those exceptions on its actual Thread. 您必须在其实际线程上处理这些异常。 But if you want to Handle them on your global exception handler you can pass it to the main thread: 但是,如果要在全局异常处理程序上处理它们,则可以将其传递给主线程:

 System.Threading.Thread t = new System.Threading.Thread(() =>
    {
        try
        {
            ...
            //this exception will not be catched by 
            //Application.DispatcherUnhandledException
            throw new Exception("huh..");
            ...
        }
        catch (Exception ex)
        {
            //But we can handle it in the throwing thread
            //and pass it to the main thread wehre Application.
            //DispatcherUnhandledException can handle it
            System.Windows.Application.Current.Dispatcher.Invoke(
                System.Windows.Threading.DispatcherPriority.Normal,
                new Action<Exception>((exc) =>
                    {
                      throw new Exception("Exception from another Thread", exc);
                    }), ex);
        }
    });

#6楼

As mentioned above 正如刚才提到的

Application.Current.DispatcherUnhandledException will not catch exceptions that are thrown from another thread then the main thread. Application.Current.DispatcherUnhandledException将不会捕获从另一个线程(然后是主线程)引发的异常。

That actual depend on how the thread was created 实际情况取决于线程的创建方式

One case that is not handled by Application.Current.DispatcherUnhandledException is System.Windows.Forms.Timer for which Application.ThreadException can be used to handle these if you run Forms on other threads than the main thread you will need to set Application.ThreadException from each such thread Application.Current.DispatcherUnhandledException未处理的一种情况是System.Windows.Forms.Timer,如果您在除主线程之外的其他线程上运行Forms,则可以使用Application.ThreadException来处理这些情况,您需要设置Application.ThreadException从每个这样的线程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值