private void RegisterWithSource(CancellationSource source)
 {
     if (source == null)
     {
         return; //todo: throw?
     }
     source.Register(Cancel);
 }
 /// <summary>
 /// Merges two CancellationSources. Treat null as NON-cancellable.
 /// </summary>
 /// <param name="source1"></param>
 /// <param name="source2"></param>
 /// <returns></returns>
 public static CancellationSource Merge(CancellationSource source1, CancellationSource source2)
 {
     if (source1 == null)
     {
         return(source2);
     }
     return(source2 == null ? source1 : new Merged(source1, source2));
 }
Exemple #3
0
 public void MergeNullSources_Fact()
 {
     CancellationSource.Merge(null, null)
     .ShouldBeNull();
     CancellationSource.Merge(CancellationSource.Canceled, null)
     .ShouldEqual(CancellationSource.Canceled);
     CancellationSource.Merge(null, CancellationSource.Canceled)
     .ShouldEqual(CancellationSource.Canceled);
 }
Exemple #4
0
        public void RegisterBeforeCancel_Fact()
        {
            object callbackCreated = null;
            var    source          = new CancellationSource();

            source.Token.Register(() => callbackCreated = new object());
            callbackCreated.ShouldBeNull();
            source.Cancel();
            callbackCreated.ShouldNotBeNull();
        }
Exemple #5
0
        public void MergeAndCancelSecond_Fact()
        {
            var source1 = new CancellationSource();
            var source2 = new CancellationSource();
            var merged  = CancellationSource.Merge(source1, source2);

            source2.Cancel();
            source1.IsCancelationRequested.ShouldBeFalse();
            source2.IsCancelationRequested.ShouldBeTrue();
            merged.IsCancelationRequested.ShouldBeTrue();
            //ensure safety in calling other cancels
            source1.Cancel();
            merged.Cancel();
        }
Exemple #6
0
        public void DisposedThrows_Fact()
        {
            CancellationSource source;

            using (source = new CancellationSource()) {
                source.IsCancelationRequested.ShouldBeFalse();
            }
            try {
                source.Cancel();
            }
            catch (ObjectDisposedException) {
                return;
            }
            throw new Exception("CancellationSource not disposed.");
        }
Exemple #7
0
        public void CancelationSourceLifecycle_Fact()
        {
            CancellationSource source;

            using (source = new CancellationSource()) {
                source.IsCancelationRequested.ShouldBeFalse();
                source.IsDisposed.ShouldBeFalse();
                source.Cancel();
                source.IsCancelationRequested.ShouldBeTrue();
                source.IsDisposed.ShouldBeFalse();
                source.Cancel();
                source.IsCancelationRequested.ShouldBeTrue();
                source.IsDisposed.ShouldBeFalse();
            }
            source.IsDisposed.ShouldBeTrue();
        }
Exemple #8
0
        public void WaitTimeoutOccurs_Fact()
        {
            var cancellationSource = new CancellationSource();
            var task = Task.Run(token => {
                while (!token.IsCancellationRequested)
                {
                    Thread.Sleep(10);
                }
            },
                                cancellationSource);
            bool waitSuccess = task.Wait(15);

            waitSuccess.ShouldBeFalse();
            cancellationSource.Cancel();
            EnsureQuietDisposal(task);
        }
Exemple #9
0
        public void CancelActionTask_NoisyWait_Fact()
        {
            var cancellationSource = new CancellationSource();
            var task = Task.Run(token => {
                for (int i = 0; i < 1000000; i++)
                {
                    token.ThrowIfCancellationRequested();
                    Thread.Sleep(10);
                }
            },
                                cancellationSource);

            Thread.Sleep(15);
            cancellationSource.Cancel();
            var waitException = Trap.WaitException(task);

            ValidateCancellationException(waitException);
            ValidateCanceledTask(task);
            EnsureQuietDisposal(task);
        }
Exemple #10
0
        public void CancelActionTask_QuietBackground_Fact()
        {
            var cancellationSource = new CancellationSource();
            var task = Task.Run(token => {
                for (int i = 0; i < 1000000; i++)
                {
                    token.ThrowIfCancellationRequested();
                    Thread.Sleep(10);
                }
            },
                                cancellationSource);

            Thread.Sleep(15);
            cancellationSource.Cancel();
            while (!task.IsComplete)
            {
                Thread.Sleep(5);
            }
            ValidateCanceledTask(task);
            EnsureQuietDisposal(task);
        }
Exemple #11
0
        public void ActionTask_CancelTaskAndActionContinuation_Fact()
        {
            var cs   = new CancellationSource();
            var task = Task.Run(token => {
                cs.Cancel();
                Thread.Sleep(10);
                token.ThrowIfCancellationRequested();
                throw new Exception("We should have been canceled, and never see this.");
            },
                                cs);
            var continuation = task.ContinueWith((previous, token) => {
                // we should never see the following exception
                throw new Exception("Pre canceled continuation should not run");
            },
                                                 cs);

            continuation.Wait();
            task.Status.ShouldEqual(TaskStatus.Canceled);
            continuation.Status.ShouldEqual(TaskStatus.Canceled);
            EnsureQuietDisposal(task);
            EnsureQuietDisposal(continuation);
        }
Exemple #12
0
 public Merged(CancellationSource source1, CancellationSource source2)
 {
     RegisterWithSource(source1);
     RegisterWithSource(source2);
 }
Exemple #13
0
 public static CancellationSource operator +(CancellationToken token1, CancellationToken token2)
 {
     return(CancellationSource.Merge(token1._source, token2._source));
 }
Exemple #14
0
 internal CancellationToken(CancellationSource source)
 {
     _source = source;
 }