如何使用任务并行库时,处理所有未处理的异常? [英] How to handle all unhandled exceptions when using Task Parallel Library?

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

问题描述

我使用TPL(任务并行库)在.NET 4.0中。我希望能够通过使用 Thread.GetDomain()来集中所有未处理的异常的处理逻辑。UnhandledException 事件。然而,在我的应用程序,该事件从来没有发射线程开始与第三方物流code,例如: Task.Factory.StartNew(...)。该事件确实解雇,如果我使用类似新的Thread(的ThreadStart)。开始()

I'm using the TPL (Task Parallel Library) in .NET 4.0. I want to be able 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文章建议使用任务#等待()赶 AggregateException 与第三方物流合作的时候,但是这不是我想要的,因为它不是集中足够的机制。

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

有没有人经历同样的问题都还是只有我?你有这个什么解决办法?

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

推荐答案

好像没有内置的方式来处理这个问题(并没有回答这个问题,经过近2周)。我已经推出了一些自定义的code利用这一服务。该解决方案的描述是pretty的漫长,所以我贴在我的博客。请参照<一个href="http://www.buunguyen.net/blog/handle-all-uncaught-exceptions-thrown-when-using-task-parallel-library.html">this帖子如果你有兴趣。

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日:我已经找到一个更好的办法来做到这一点,利用工作的延续。我创建了一个类的ThreadFactory 暴露可以通过顶层处理程序进行订阅,并提供方法来启动安装有适当的延续任务的错误事件。
在code张贴<一个href="http://www.buunguyen.net/blog/handle-all-uncaught-exceptions-thrown-when-using-task-parallel-library-take-2.html">here.

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:发表code从博客文章按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();
    }
}

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

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