C# Task WhenAny和WhenAll 以及TaskFactory 的ContinueWhenAny和ContinueWhenAll的实现

个人感觉Task 的WaitAny和WhenAny以及TaskFactory 的ContinueWhenAny有相似的地方,而WaitAll和WhenAll以及TaskFactory 的ContinueWhenAll也是相同,但是WaitAny和WhenAny的返回值有所不同。我们首先来看看Task WhenAny和WhenAll 的实现吧,

public class Task : IThreadPoolWorkItem, IAsyncResult, IDisposable
{
    //Creates a task that will complete when any of the supplied tasks have completed.
    public static Task<Task> WhenAny(IEnumerable<Task> tasks)
    {
        if (tasks == null) throw new ArgumentNullException("tasks");
        Contract.EndContractBlock();
        List<Task> taskList = new List<Task>();
        foreach (Task task in tasks)
        {
            if (task == null) throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_NullTask"), "tasks");
            taskList.Add(task);
        }
        if (taskList.Count == 0)
        {
            throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_EmptyTaskList"), "tasks");
        }
        // Previously implemented CommonCWAnyLogic() can handle the rest
        return TaskFactory.CommonCWAnyLogic(taskList);
    }
    
    //Creates a task that will complete when all of the supplied tasks have completed.
    public static Task<TResult[]> WhenAll<TResult>(params Task<TResult>[] tasks)
    {
        if (tasks == null) throw new ArgumentNullException("tasks");
        Contract.EndContractBlock();

        int taskCount = tasks.Length;
        if (taskCount == 0) return InternalWhenAll<TResult>(tasks); // small optimization in the case of an empty task array

        Task<TResult>[] tasksCopy = new Task<TResult>[taskCount];
        for (int i = 0; i < taskCount; i++)
        {
            Task<TResult> task = tasks[i];
            if (task == null) throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_NullTask"), "tasks");
            tasksCopy[i] = task;
        }
        return InternalWhenAll<TResult>(tasksCopy);
    }
        
    private static Task<TResult[]> InternalWhenAll<TResult>(Task<TResult>[] tasks)
    {
        Contract.Requires(tasks != null, "Expected a non-null tasks array");
        return (tasks.Length == 0) ? new Task<TResult[]>(false, new TResult[0], TaskCreationOptions.None, default(CancellationToken)) : new WhenAllPromise<TResult
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值