当前位置: 首页>>代码示例>>C#>>正文


C# BackgroundWorker.CancelAsync方法代码示例

本文整理汇总了C#中BackgroundWorker.CancelAsync方法的典型用法代码示例。如果您正苦于以下问题:C# BackgroundWorker.CancelAsync方法的具体用法?C# BackgroundWorker.CancelAsync怎么用?C# BackgroundWorker.CancelAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BackgroundWorker的用法示例。


在下文中一共展示了BackgroundWorker.CancelAsync方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestCancelAsync

        public void TestCancelAsync()
        {
            BackgroundWorker bw = new BackgroundWorker();

            bw.DoWork += DoWorkExpectCancel;
            bw.WorkerSupportsCancellation = true;

            manualResetEvent3 = new ManualResetEventSlim(false);

            bw.RunWorkerAsync("Message");
            bw.CancelAsync();

            bool ret = manualResetEvent3.Wait(TimeoutLong);
            Assert.True(ret);
            // there could be race condition between worker thread cancellation and completion which will set the CancellationPending to false 
            // if it is completed already, we don't check cancellation
            if (bw.IsBusy) // not complete
            {
                if (!bw.CancellationPending)
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        Wait(TimeoutShort);
                        if (bw.CancellationPending)
                        {
                            break;
                        }
                    }
                }
                // Check again
                if (bw.IsBusy)
                    Assert.True(bw.CancellationPending, "Cancellation in Main thread");
            }
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:34,代码来源:BackgroundWorkerTests.cs

示例2: CancelAsyncNonBusy

 public void CancelAsyncNonBusy()
 {
     var backgroundWorker = new BackgroundWorker<object, object, object>
                 {
                     WorkerSupportsCancellation = true
                 };
     Assert.IsFalse(backgroundWorker.IsBusy);
     backgroundWorker.CancelAsync();
 }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:9,代码来源:BackgroundWorkerTest.cs

示例3: TestCancelAsyncWithoutCancellationSupport

 public void TestCancelAsyncWithoutCancellationSupport()
 {
     var bw = new BackgroundWorker() { WorkerSupportsCancellation = false };
     Assert.Throws<InvalidOperationException>(() => bw.CancelAsync());
 }
开发者ID:noahfalk,项目名称:corefx,代码行数:5,代码来源:BackgroundWorkerTests.cs

示例4: TestCancelInsideDoWork

        public void TestCancelInsideDoWork()
        {
            var original = SynchronizationContext.Current;
            try
            {
                SynchronizationContext.SetSynchronizationContext(null);

                var bw = new BackgroundWorker() { WorkerSupportsCancellation = true };
                var barrier = new Barrier(2);

                bw.DoWork += (sender, e) =>
                {
                    barrier.SignalAndWait();
                    barrier.SignalAndWait();

                    if (bw.CancellationPending)
                    {
                        e.Cancel = true;
                    }
                };

                bw.RunWorkerCompleted += (sender, e) =>
                {
                    Assert.True(e.Cancelled);
                    barrier.SignalAndWait();
                };

                bw.RunWorkerAsync();
                barrier.SignalAndWait();
                bw.CancelAsync();
                barrier.SignalAndWait();
                Assert.True(barrier.SignalAndWait(TimeoutLong), "Background work timeout");
            }
            finally
            {
                SynchronizationContext.SetSynchronizationContext(original);
            }
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:38,代码来源:BackgroundWorkerTests.cs

示例5: BackgroundWorker_CancelAsync_ReportsCancelledWhenWorkerSupportsCancellationIsTrue

        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();
        }
开发者ID:BiYiTuan,项目名称:csla,代码行数:32,代码来源:BackgroundWorkerTests.cs

示例6: BackgroundWorker_CancelAsync_ThrowsInvalidOperationExceptionWhenWorkerSupportsCancellationIsFalse

        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);
            }
            context.Complete();
              }
        }
开发者ID:BiYiTuan,项目名称:csla,代码行数:26,代码来源:BackgroundWorkerTests.cs

示例7: CancelAsyncNoCancellationSupported

 public void CancelAsyncNoCancellationSupported()
 {
     var backgroundWorker = new BackgroundWorker<object, object, object>();
     Assert.IsFalse(backgroundWorker.IsBusy);
     backgroundWorker.CancelAsync();
 }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:6,代码来源:BackgroundWorkerTest.cs


注:本文中的BackgroundWorker.CancelAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。