Пример #1
0
        public void catch_happens_before_finally_with_CurrentThreadScheduler()
        {
            var task1HasRun = false;
            var task2HasRun = false;
            var task3HasRun = false;

            var scheduler = new CurrentThreadTaskScheduler();

            new TaskFactory(scheduler)
                .StartNew(() =>
                          {
                              Assert.That(task1HasRun, Is.False);
                              Assert.That(task2HasRun, Is.False);
                              Assert.That(task3HasRun, Is.False);
                              task1HasRun = true;

                              throw new Exception();
                          })
                .CatchAndHandle(_ =>
                                {
                                    Assert.That(task1HasRun, Is.True);
                                    Assert.That(task2HasRun, Is.False);
                                    Assert.That(task3HasRun, Is.False);
                                    task2HasRun = true;
                                }, scheduler)
                .Finally(() =>
                         {
                             Assert.That(task1HasRun, Is.True);
                             Assert.That(task2HasRun, Is.True);
                             Assert.That(task3HasRun, Is.False);
                             task3HasRun = true;
                         }, scheduler);

            Assert.That(task1HasRun, Is.True);
            Assert.That(task2HasRun, Is.True);
            Assert.That(task3HasRun, Is.True);
        }
Пример #2
0
 public TestIOCompletionScheduler()
 {
     TPL = new CurrentThreadTaskScheduler();
 }
Пример #3
0
 public TestDispatcherScheduler()
 {
     TPL = new CurrentThreadTaskScheduler();
     RX = new TestScheduler();
 }
Пример #4
0
        public void when_SelectMany_and_two_schedulers_then_they_are_called_in_order_overload6()
        {
            var autoResetEvent = new AutoResetEvent(false);

            var task1HasRun = false;
            var task2HasRun = false;

            var taskScheduler = TaskScheduler.Default;
            var currentThreadTaskScheduler = new CurrentThreadTaskScheduler();

            new TaskFactory(taskScheduler)
                .StartNew(() =>
                          {
                              Assert.That(task1HasRun, Is.False);

                              task1HasRun = true;
                          }, taskScheduler)
                .Then(() =>
                      {
                          Assert.That(task1HasRun, Is.True);
                          Assert.That(task2HasRun, Is.False);

                          task2HasRun = true;

                          return CompletedTask.Default;
                      }, currentThreadTaskScheduler)
                .Finally(() => autoResetEvent.Set(), taskScheduler);

            autoResetEvent.WaitOne();

            Assert.That(task1HasRun, Is.True);
            Assert.That(task2HasRun, Is.True);
        }
Пример #5
0
        public void catch_happens_before_finally_with_two_schedulers()
        {
            var autoResetEvent = new AutoResetEvent(false);

            var task1HasRun = false;
            var task2HasRun = false;
            var task3HasRun = false;

            var taskScheduler = TaskScheduler.Default;
            var currentTaskScheduler = new CurrentThreadTaskScheduler();

            new TaskFactory(taskScheduler)
                .StartNew(() =>
                          {
                              Assert.That(task1HasRun, Is.False);
                              Assert.That(task2HasRun, Is.False);
                              Assert.That(task3HasRun, Is.False);
                              task1HasRun = true;

                              throw new Exception();
                          })
                .CatchAndHandle(_ =>
                                {
                                    Assert.That(task1HasRun, Is.True);
                                    Assert.That(task2HasRun, Is.False);
                                    Assert.That(task3HasRun, Is.False);
                                    task2HasRun = true;
                                }, taskScheduler)
                .Finally(() =>
                         {
                             Assert.That(task1HasRun, Is.True);
                             Assert.That(task2HasRun, Is.True);
                             Assert.That(task3HasRun, Is.False);
                             task3HasRun = true;

                             autoResetEvent.Set();
                         }, currentTaskScheduler);

            autoResetEvent.WaitOne();

            Assert.That(task1HasRun, Is.True);
            Assert.That(task2HasRun, Is.True);
            Assert.That(task3HasRun, Is.True);
        }