當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。