// parent is the outer task in which this task is created
readonly Task parent;
- // contAncestor is the Task on which this continuation was setup
- readonly Task contAncestor;
+ // A reference to a Task on which this continuation is attached to
+ Task contAncestor;
static int id = -1;
static readonly TaskFactory defaultFactory = new TaskFactory ();
void InnerInvoke ()
{
if (IsContinuation) {
- invoker.Invoke (contAncestor, state, this);
+ var ancestor = contAncestor;
+ contAncestor = null;
+ invoker.Invoke (ancestor, state, this);
} else {
invoker.Invoke (this, state, this);
}
if (millisecondsDelay < -1)
throw new ArgumentOutOfRangeException ("millisecondsDelay");
- var task = new Task (TaskActionInvoker.Delay, millisecondsDelay, cancellationToken, TaskCreationOptions.None, null, TaskConstants.Finished);
+ if (cancellationToken.IsCancellationRequested)
+ return TaskConstants.Canceled;
+
+ var task = new Task (TaskActionInvoker.Empty, null, cancellationToken, TaskCreationOptions.None, null, null);
task.SetupScheduler (TaskScheduler.Default);
-
- if (millisecondsDelay != Timeout.Infinite)
- task.scheduler.QueueTask (task);
+
+ if (millisecondsDelay != Timeout.Infinite) {
+ var timer = new Timer (delegate (object state) {
+ var t = (Task) state;
+ if (t.Status == TaskStatus.WaitingForActivation) {
+ t.Status = TaskStatus.Running;
+ t.Finish ();
+ }
+ }, task, millisecondsDelay, -1);
+
+ task.ContinueWith (new DisposeContinuation (timer));
+ }
return task;
}
internal static Task<TResult[]> WhenAllCore<TResult> (IList<Task<TResult>> tasks)
{
+ if (tasks.Count == 0)
+ return FromResult(new TResult[0]);
+
foreach (var t in tasks) {
if (t == null)
throw new ArgumentException ("tasks", "the tasks argument contains a null element");