本文整理汇总了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");
}
}
示例2: CancelAsyncNonBusy
public void CancelAsyncNonBusy()
{
var backgroundWorker = new BackgroundWorker<object, object, object>
{
WorkerSupportsCancellation = true
};
Assert.IsFalse(backgroundWorker.IsBusy);
backgroundWorker.CancelAsync();
}
示例3: TestCancelAsyncWithoutCancellationSupport
public void TestCancelAsyncWithoutCancellationSupport()
{
var bw = new BackgroundWorker() { WorkerSupportsCancellation = false };
Assert.Throws<InvalidOperationException>(() => bw.CancelAsync());
}
示例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);
}
}
示例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();
}
示例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();
}
}
示例7: CancelAsyncNoCancellationSupported
public void CancelAsyncNoCancellationSupported()
{
var backgroundWorker = new BackgroundWorker<object, object, object>();
Assert.IsFalse(backgroundWorker.IsBusy);
backgroundWorker.CancelAsync();
}