public Task ContinueWhenAny(Task[] tasks, Action <Task> continuationAction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
        {
            if (tasks == null)
            {
                throw new ArgumentNullException("tasks");
            }

            if (tasks.Length == 0)
            {
                throw new ArgumentException("The tasks argument contains no tasks", "tasks");
            }

            foreach (var ta in tasks)
            {
                if (ta == null)
                {
                    throw new ArgumentException("The tasks argument constains a null value", "tasks");
                }
            }

            if (continuationAction == null)
            {
                throw new ArgumentNullException("continuationAction");
            }

            var t = new Task <int> (l =>
            {
                var data = (Tuple <Task[], CancellationToken>)l;
                return(Task.WaitAny(data.Item1, data.Item2));
            }, Tuple.Create(tasks, cancellationToken));

            var cont = t.ContinueWith(TaskActionInvoker.Create(continuationAction, tasks), cancellationToken, continuationOptions, scheduler);

            t.Start(scheduler);

            return(cont);
        }
Пример #2
0
        internal static void HandleOnCompleted(Task task, Action continuation, bool continueOnSourceContext, bool manageContext)
        {
            if (continueOnSourceContext && SynchronizationContext.Current != null && SynchronizationContext.Current.GetType() != typeof(SynchronizationContext))
            {
                task.ContinueWith(new SynchronizationContextContinuation(continuation, SynchronizationContext.Current));
            }
            else
            {
                IContinuation cont;
                Task          cont_task;
                if (continueOnSourceContext && !TaskScheduler.IsDefault)
                {
                    cont_task = new Task(TaskActionInvoker.Create(continuation), null, CancellationToken.None, TaskCreationOptions.None, null);
                    cont_task.SetupScheduler(TaskScheduler.Current);
                    cont = new SchedulerAwaitContinuation(cont_task);
                }
                else
                {
                    cont_task = null;
                    cont      = new AwaiterActionContinuation(continuation);
                }

                if (task.ContinueWith(cont, false))
                {
                    return;
                }

                if (cont_task == null)
                {
                    cont_task = new Task(TaskActionInvoker.Create(continuation), null, CancellationToken.None, TaskCreationOptions.None, null);
                    cont_task.SetupScheduler(TaskScheduler.Current);
                }

                cont_task.Schedule(true);
            }
        }