我在實現以下方面遇到了一些麻煩:C#事件冒泡
Form1有兩個按鈕「驗證」和「取消」,也有一個BackgroundWorker。 Class1有一個繁重的處理方法。
單擊「驗證」調用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告訴另一個類對象的方法,取消被點擊。 – Libas