【发布时间】:2011-11-08 20:32:37
【问题描述】:
我在实现以下目标时遇到了一些麻烦:
Form1 有两个按钮“Validate”和“Cancel”,还有一个BackgroundWorker。 Class1 处理方法繁重。
点击“Validate”调用DoWork,繁重的工作是Class1的一个方法。我已经设法“监听”了我的 DoWork 中由 Class1 方法引起的进度更改事件。
现在,当单击“取消”按钮时,我正在尝试取消繁重的任务(在方法内部)。
private void buttonCancelValidation_Click(object sender, EventArgs e)
{
backgroundWorker.CancelAsync();
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
Class1 obj = new Class1();
obj.ProgressChanged += (s, pe) => backgroundWorker.ReportProgress(pe.ProgressPercentage);
//---> Here a i'm trying to "tell" class1 that if the "Cancel" button was clicked then I want to abort ASAP the HeavyMethod operation.
obj.HeavyMethod();
//followed by the cancel of BackgroundWorker DoWork
if (backgroundWorker.CancellationPending
{
e.Cancel = true;
}
}
感谢您的帮助!
【问题讨论】:
-
@Charles 如何从 FORM1 告诉另一个类对象方法单击了取消。
标签: c# winforms events backgroundworker