本文整理匯總了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();
}