【问题标题】:What is the best way to catch exception in Task?在任务中捕获异常的最佳方法是什么?
【发布时间】:2012-10-10 10:47:19
【问题描述】:

使用System.Threading.Tasks.Task<TResult>,我必须管理可能引发的异常。我正在寻找最好的方法来做到这一点。到目前为止,我已经创建了一个基类来管理 .ContinueWith(...) 调用中所有未捕获的异常

我想知道是否有更好的方法来做到这一点。或者即使这是一个很好的方法。

public class BaseClass
{
    protected void ExecuteIfTaskIsNotFaulted<T>(Task<T> e, Action action)
    {
        if (!e.IsFaulted) { action(); }
        else
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
            {
                /* I display a window explaining the error in the GUI 
                 * and I log the error.
                 */
                this.Handle.Error(e.Exception);
            }));            
        }
    }
}   

public class ChildClass : BaseClass
{
    public void DoItInAThread()
    {
        var context = TaskScheduler.FromCurrentSynchronizationContext();
        Task.Factory.StartNew<StateObject>(() => this.Action())
                    .ContinueWith(e => this.ContinuedAction(e), context);
    }

    private void ContinuedAction(Task<StateObject> e)
    {
        this.ExecuteIfTaskIsNotFaulted(e, () =>
        {
            /* The action to execute 
             * I do stuff with e.Result
             */

        });        
    }
}

【问题讨论】:

    标签: c# .net task-parallel-library


    【解决方案1】:

    有两种方法可以做到这一点,具体取决于您使用的语言版本。

    C# 5.0 及以上

    您可以使用async 和await 关键字来为您简化大部分操作。

    在语言中引入了async 和await 以简化Task Parallel Library 的使用,避免您必须使用ContinueWith 并允许您继续以自上而下的方式进行编程。

    因此,您可以简单地使用 try/catch 块来捕获异常,如下所示:

    try
    {
        // Start the task.
        var task = Task.Factory.StartNew<StateObject>(() => { /* action */ });
    
        // Await the task.
        await task;
    }
    catch (Exception e)
    {
        // Perform cleanup here.
    }
    

    请注意,封装上述方法必须使用async关键字,以便您可以使用await。

    C# 4.0 及以下

    您可以使用从TaskContinuationOptions enumeration 获取值的ContinueWith overload 处理异常,如下所示:

    // Get the task.
    var task = Task.Factory.StartNew<StateObject>(() => { /* action */ });
    
    // For error handling.
    task.ContinueWith(t => { /* error handling */ }, context,
        TaskContinuationOptions.OnlyOnFaulted);
    

    TaskContinuationOptions 枚举的OnlyOnFaulted 成员表示只有在前面的任务抛出异常时才应该执行继续。

    当然,您可以在同一个前提条件下多次调用ContinueWith,处理非异常情况:

    // Get the task.
    var task = new Task<StateObject>(() => { /* action */ });
    
    // For error handling.
    task.ContinueWith(t => { /* error handling */ }, context, 
        TaskContinuationOptions.OnlyOnFaulted);
    
    // If it succeeded.
    task.ContinueWith(t => { /* on success */ }, context,
        TaskContinuationOptions.OnlyOnRanToCompletion);
    
    // Run task.
    task.Start();
    

    【讨论】:

    • 如何知道匿名方法中的异常类型?如果我这样做 t.Exception ,智能感知不会暴露内部异常、消息...等属性...
    • @guiomie t 是例外。
    • 上下文未定义是什么?
    • @MonsterMMORPG SynchronizationContext,如果需要的话。
    • 回答为什么我们需要它?我的意思是在哪种情况下?这够了吗 ? myTask.ContinueWith(t => ErrorLogger.LogError("错误发生在 func_CheckWaitingToProcessPages 启动任务和错误:" + t), TaskContinuationOptions.OnlyOnFaulted);
    【解决方案2】:

    您可以创建一些自定义任务工厂,该工厂将生成嵌入了异常处理处理的任务。像这样的:

    using System;
    using System.Threading.Tasks;
    
    class FaFTaskFactory
    {
        public static Task StartNew(Action action)
        {
            return Task.Factory.StartNew(action).ContinueWith(
                c =>
                {
                    AggregateException exception = c.Exception;
    
                    // Your Exception Handling Code
                },
                TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously
            ).ContinueWith(
                c =>
                {
                    // Your task accomplishing Code
                },
                TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously
            );
        }
    
        public static Task StartNew(Action action, Action<Task> exception_handler, Action<Task> completion_handler)
        {
            return Task.Factory.StartNew(action).ContinueWith(
                exception_handler,
                TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously
            ).ContinueWith(
                completion_handler,
                TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously
            );
        }
    };
    

    您可以在客户端代码中忘记对该工厂生成的任务的异常处理。同时,您仍然可以等待此类任务的完成或以“即发即弃”的方式使用它们:

    var task1 = FaFTaskFactory.StartNew( () => { throw new NullReferenceException(); } );
    var task2 = FaFTaskFactory.StartNew( () => { throw new NullReferenceException(); },
                                          c => {    Console.WriteLine("Exception!"); },
                                          c => {    Console.WriteLine("Success!"  ); } );
    
    task1.Wait(); // You can omit this
    task2.Wait(); // You can omit this
    

    但老实说,我不确定你为什么想要完成处理代码。无论如何,此决定取决于您的应用程序的逻辑。

    【讨论】:

      猜你喜欢
      • 2010-09-10
      • 1970-01-01
      • 2022-08-03
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      相关资源
      最近更新 更多