【发布时间】:2023-03-23 14:51:01
【问题描述】:
我有一个作为主线程的 GUI 表单,而我有一个不同的类用于需要完成的实际工作。
有没有办法正确检查工人的CancellationPending 属性,因为它是从不同的类激活的,除了将工人作为“DoJob”方法的参数传递以便它可以检查属性?
代码(在主类中):
// This method is registered as the DoWork method for the worker
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
workClassInstance.DoJob();
}
private void buttonCancel_Click(object sender, RoutedEventArgs e)
{
if (bw.WorkerSupportsCancellation == true)
{
bw.CancelAsync();
}
}
【问题讨论】:
标签: c# backgroundworker cancellation