コード例 #1
0
        public void Dispose_ClearsPendingTasks()
        {
            var scheduler = new TaskDelayScheduler(TimeSpan.FromMilliseconds(50), new IProjectThreadingServiceMock(), CancellationToken.None);

            bool taskRan = false;
            var  task1   = scheduler.ScheduleAsyncTask((ct) =>
            {
                taskRan   = true;
                int count = 50;
                while (count != 0)
                {
                    ct.ThrowIfCancellationRequested();
                    Thread.Sleep(20);
                    --count;
                }
                return(Task.CompletedTask);
            });

            scheduler.Dispose();

            // There are two cases to consider in the verification. One, the cancellation is detected by the scheduler. In that case
            // the task will complete successfully (it does not cancel itself), but the inner async task will not run.  And 2, the cancellation
            // occurs after the scheduling starts. In that case the inner task will execute, but is crafted to throw and OperationCancelledException.
            try
            {
                task1.Task.Wait();
                Assert.False(taskRan);
            }
            catch (OperationCanceledException)
            {
            }
        }
コード例 #2
0
        public async Task Dispose_SkipsPendingTasks()
        {
            using var scheduler = new TaskDelayScheduler(TimeSpan.FromMilliseconds(250), IProjectThreadingServiceFactory.Create(), CancellationToken.None);
            bool taskRan = false;
            var task = scheduler.ScheduleAsyncTask(ct =>
            {
                taskRan = true;
                return Task.CompletedTask;
            });

            scheduler.Dispose();

            await task;
            Assert.False(taskRan);
        }