Пример #1
1
        public void BackgroundWorker_CancelAsync_ReportsCancelledWhenWorkerSupportsCancellationIsTrue()
        {
            UnitTestContext context = GetContext();

              //int numTimesProgressCalled = 0;

              BackgroundWorker target = new BackgroundWorker();
              target.DoWork += (o, e) =>
              {
            // report progress changed 10 times
            for (int i = 1; i < 11; i++)
            {
              Thread.Sleep(100);
              if (target.CancellationPending)
              {
            e.Cancel = true;
            return;
              }
            }
              };
              target.WorkerSupportsCancellation = true;
              target.RunWorkerCompleted += (o, e) =>
              {
            //  target does not support ReportProgress we shold get a System.InvalidOperationException from DoWork
            context.Assert.IsNull(e.Error);
            context.Assert.IsTrue(e.Cancelled);
            context.Assert.Success();
              };
              target.RunWorkerAsync(null);
              target.CancelAsync();
              context.Complete();
        }
Пример #2
1
        public void BackgroundWorker_CancelAsync_ThrowsInvalidOperationExceptionWhenWorkerSupportsCancellationIsFalse()
        {
            using (UnitTestContext context = GetContext())
              {
            BackgroundWorker target = new BackgroundWorker();
            target.DoWork += (o, e) =>
            {
              for (int i = 1; i < 11; i++)
              {
            Thread.Sleep(10);
              }
            };
            target.WorkerSupportsCancellation = false;
            target.RunWorkerAsync(null);

            try
            {
              target.CancelAsync();   // this call throws exception
            }
            catch (InvalidOperationException ex)
            {
              context.Assert.Fail(ex);
            }
              }
        }