使用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# 5.0 and above

你可以使用asyncawait个关键词来简化这一过程.

asyncawait被引入到语言中,以简化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.
}

请注意,封装上述must用法的方法应用了关键字async,因此您可以使用await.

C# 4.0 and below

您可以使用从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成员指示如果Antecedent任务引发异常,则应该执行only继续.

当然,在处理非特殊情况时,您可以在同一个先行条件下拨打多个电话至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();

.net相关问答推荐

.NET模拟具有泛型返回类型的方法

如何将 signalR 添加到不同项目中的后台服务?

在 C# 中生成随机小数

SqlDateTime.MinValue != DateTime.MinValue,为什么?

将 BitmapImage 转换为 Bitmap,反之亦然

C#:内存不足异常

Linq查询分组并 Select 第一个项目

.net:System.Web.Mail 与 System.Net.Mail

如何从 .net 中的数组类型获取数组项类型

DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") 返回上午时间而不是下午时间?

C# 编译为 32/64 位,或任何 cpu?

参数命名:文件名还是文件名?

风格上的差异:IDictionary vs Dictionary

log4net的正确使用方法(记录器命名)

使用 lambda 表达式代替 IComparer 参数

并发字典正确用法

例外:不支持 URI 格式

为什么要使用 C# 类 System.Random 而不是 System.Security.Cryptography.RandomNumberGenerator?

如果选中,则更改列表框项的 WPF DataTemplate

WPF 中的 Application.DoEvents() 在哪里?