Пример #1
0
        public void fires_complete_event_when_work_is_cancelled()
        {
            bool wasFired = false;

            var            handle = new ManualResetEvent(false);
            BackgroundTask task   = null;

            task = new BackgroundTask(
                () => {
                task.Cancel();
                return(null);
            });

            task.Completed +=
                (s, e) =>
            {
                wasFired = true;
                s.ShouldBe(task);
                Action resultAction = () =>
                {
                    var result = e.Result;
                };
                resultAction.ShouldThrow <Exception>();
                e.Error.ShouldBeNull();
                e.Cancelled.ShouldBeTrue();

                handle.Set();
            };

            task.Start(null);

            handle.WaitOne(1000);
            wasFired.ShouldBeTrue();
        }
Пример #2
0
        public void when_cancelled_before_start_will_not_execute_the_delegate()
        {
            bool wasExecuted = false;

            var task = new BackgroundTask(() => wasExecuted = true);

            task.Cancel();
            task.Start(null);

            wasExecuted.ShouldBeFalse();
        }
Пример #3
0
        public void when_cancelled_during_execution_will_update_context()
        {
            BackgroundTask task = null;

            task = new BackgroundTask(
                () => {
                task.Cancel();
                BackgroundTask.CurrentContext.CancellationPending.ShouldBeTrue();
                task.CancellationPending.ShouldBeTrue();
                return(null);
            });

            task.Start(null);
        }
Пример #4
0
        public void when_cancelled_before_start_will_not_execute_the_delegate()
        {
            bool wasExecuted = false;

            var task = new BackgroundTask(() => wasExecuted = true);
            task.Cancel();
            task.Start(null);

            Assert.That(wasExecuted, Is.False);
        }