public void SetCanceled() { if (!ApplyOperation(TaskStatus.Canceled, () => { source.Cancel(); source.CancelReal(); })) { ThrowInvalidException(); } }
internal void ContinueWithCore(Task continuation, TaskContinuationOptions kind, TaskScheduler scheduler, Func <bool> predicate) { // Already set the scheduler so that user can call Wait and that sort of stuff continuation.taskScheduler = scheduler; continuation.scheduler = ProxifyScheduler(scheduler); AtomicBoolean launched = new AtomicBoolean(); EventHandler action = delegate { if (!predicate()) { return; } if (!launched.Value && !launched.Exchange(true)) { if (!ContinuationStatusCheck(kind)) { continuation.Cancel(); continuation.CancelReal(); continuation.Dispose(); return; } CheckAndSchedule(continuation, kind, scheduler); } }; if (IsCompleted) { action(this, EventArgs.Empty); return; } completed += action; // Retry in case completion was achieved but event adding was too late if (IsCompleted) { action(this, EventArgs.Empty); } }