使用任务并行库时如何处理所有未处理的异常

使用任务并行库时如何处理所有未处理的异常

本文介绍了使用任务并行库时如何处理所有未处理的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TPL (.NET 4.0 中的任务并行库).我想通过使用 Thread.GetDomain().UnhandledException 事件来集中所有未处理异常的处理逻辑.但是,在我的应用程序中,对于以 TPL 代码启动的线程,例如,永远不会触发该事件.Task.Factory.StartNew(...).如果我使用诸如 new Thread(threadStart).Start() 之类的东西,确实会触发该事件.

I'm using the TPL (Task Parallel Library) in .NET 4.0. I want to centralize the handling logic of all unhandled exceptions by using the Thread.GetDomain().UnhandledException event. However, in my application, the event is never fired for threads started with TPL code, e.g. Task.Factory.StartNew(...). The event is indeed fired if I use something like new Thread(threadStart).Start().

这篇 MSDN 文章建议在使用 TPL 时使用 Task.Wait() 来捕获 AggregateException ,但这不是我想要的,因为这种机制不够集中".

This MSDN article suggests to use Task.Wait() to catch the AggregateException when working with TPL, but that is not what I want because this mechanism is not "centralized" enough.

有没有人遇到过同样的问题,还是只有我遇到过同样的问题?你有什么解决办法吗?

Does anyone experience same problem at all or is it just me? Do you have any solution for this?

推荐答案

似乎没有内置的方法来处理这个问题(并且在将近 2 周后没有回答这个问题).我已经推出了一些自定义代码来解决这个问题.解决方案描述很长,所以我已经在我的博客中发布了.请参阅这篇文章 如果你有兴趣.

Seems like there's no built-in way to handle this (and no answer to this question after almost 2 weeks). I already rolled out some custom code to take care of this. The solution description is pretty lengthy, so I've posted in my blog. Refer to this post if you're interested.

2010 年 5 月 7 日更新:我找到了一种更好的方法,即利用任务延续.我创建了一个 class ThreadFactory,它公开了可以由顶级处理程序订阅的 Error 事件,并提供方法来启动附加有适当延续的任务.
代码发布在 此处.

Update 5/7/2010: I’ve found a better way to do that, making use of task continuation. I create a class ThreadFactory that exposes the Error event which can be subscribed by a top-level handler and provides methods to start a task attached with proper continuation.
The code is posted here.

2011 年 4 月 18 日更新:根据 Nifle 的评论发布博文中的代码.

Update 4/18/2011: Post code from the blog post as per Nifle's comment.

internal class ThreadFactory
{
    public delegate void TaskError(Task task, Exception error);

    public static readonly ThreadFactory Instance = new ThreadFactory();

    private ThreadFactory() {}

    public event TaskError Error;

    public void InvokeError(Task task, Exception error)
    {
        TaskError handler = Error;
        if (handler != null) handler(task, error);
    }

    public void Start(Action action)
    {
        var task = new Task(action);
        Start(task);
    }

    public void Start(Action action, TaskCreationOptions options)
    {
        var task = new Task(action, options);
        Start(task);
    }

    private void Start(Task task)
    {
        task.ContinueWith(t => InvokeError(t, t.Exception.InnerException),
                            TaskContinuationOptions.OnlyOnFaulted |
                            TaskContinuationOptions.ExecuteSynchronously);
        task.Start();
    }
}

这篇关于使用任务并行库时如何处理所有未处理的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 01:49