public Task ContinueWhenAll(Task[] tasks, Action <Task[]> continuationAction, CancellationToken cancellationToken,
                                    TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
        {
            CheckContinueArguments(tasks, continuationAction, continuationOptions, scheduler);

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

            return(cont);
        }
        public Task <TResult> ContinueWhenAll <TResult>(Task[] tasks, Func <Task[], TResult> continuationFunction,
                                                        CancellationToken cancellationToken,
                                                        TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
        {
            CheckContinueArguments(tasks, continuationFunction, continuationOptions, scheduler);

            var cont = Task.WhenAllCore(tasks).ContinueWith <TResult>(TaskActionInvoker.Create(continuationFunction, tasks), cancellationToken, continuationOptions, scheduler);

            return(cont);
        }
Exemple #3
0
        public Task ContinueWhenAll(Task[] tasks, Action <Task[]> continuationAction, CancellationToken cancellationToken,
                                    TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
        {
            var t = new Task(l => {
                var data = (Tuple <Task[], CancellationToken>)l;
                Task.WaitAll(data.Item1, data.Item2);
            }, Tuple.Create(tasks, cancellationToken));

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

            t.Start(scheduler);

            return(cont);
        }
Exemple #4
0
        public Task <TResult> ContinueWhenAny <TResult> (Task[] tasks,
                                                         Func <Task, TResult> continuationFunction,
                                                         CancellationToken cancellationToken,
                                                         TaskContinuationOptions continuationOptions,
                                                         TaskScheduler scheduler)
        {
            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 <TResult> (TaskActionInvoker.Create(continuationFunction, tasks), cancellationToken, continuationOptions, scheduler);

            t.Start(scheduler);

            return(cont);
        }
Exemple #5
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);
                }

                //
                // This is awaiter continuation. For finished tasks we get false result and need to
                // queue the continuation otherwise the task would block
                //
                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);
            }
        }
        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);
        }