共用方式為


BackgroundWorker.DoWork 事件

定義

RunWorkerAsync() 被叫到時發生。

public:
 event System::ComponentModel::DoWorkEventHandler ^ DoWork;
public event System.ComponentModel.DoWorkEventHandler DoWork;
public event System.ComponentModel.DoWorkEventHandler? DoWork;
member this.DoWork : System.ComponentModel.DoWorkEventHandler 
Public Custom Event DoWork As DoWorkEventHandler 

事件類型

範例

以下程式碼範例示範如何利用事件 DoWork 啟動非同步操作。 此程式碼範例是本類別更大範例 BackgroundWorker 的一部分。

// This event handler is where the actual,
// potentially time-consuming work is done.
void backgroundWorker1_DoWork( Object^ sender, DoWorkEventArgs^ e )
{
   // Get the BackgroundWorker that raised this event.
   BackgroundWorker^ worker = dynamic_cast<BackgroundWorker^>(sender);

   // Assign the result of the computation
   // to the Result property of the DoWorkEventArgs
   // object. This is will be available to the 
   // RunWorkerCompleted eventhandler.
   e->Result = ComputeFibonacci( safe_cast<Int32>(e->Argument), worker, e );
}
// This event handler is where the actual,
// potentially time-consuming work is done.
void backgroundWorker1_DoWork(object sender,
    DoWorkEventArgs e)
{
    // Get the BackgroundWorker that raised this event.
    BackgroundWorker worker = sender as BackgroundWorker;

    // Assign the result of the computation
    // to the Result property of the DoWorkEventArgs
    // object. This is will be available to the 
    // RunWorkerCompleted eventhandler.
    e.Result = ComputeFibonacci((int)e.Argument, worker, e);
}
' This event handler is where the actual work is done.
Private Sub backgroundWorker1_DoWork(
ByVal sender As Object,
ByVal e As DoWorkEventArgs) _
Handles backgroundWorker1.DoWork

    ' Get the BackgroundWorker object that raised this event.
    Dim worker As BackgroundWorker =
        CType(sender, BackgroundWorker)

    ' Assign the result of the computation
    ' to the Result property of the DoWorkEventArgs
    ' object. This is will be available to the 
    ' RunWorkerCompleted eventhandler.
    e.Result = ComputeFibonacci(e.Argument, worker, e)
End Sub

備註

這個事件會在你叫方法時 RunWorkerAsync 觸發。 這裡是你開始執行可能耗時工作的操作。

你在事件處理器中的 DoWork 程式碼應該定期檢查 CancellationPending 屬性值,如果是 true就中止該操作。 當發生這種情況時,你可以將 的旗標設Canceltrue,而Cancelled你的RunWorkerCompleted事件處理器中的 旗System.ComponentModel.RunWorkerCompletedEventArgs標將被設為 trueSystem.ComponentModel.DoWorkEventArgs

謹慎

請注意,事件處理程式中的 DoWork 程式碼可能會在取消請求中完成工作,輪詢迴圈可能會錯過 CancellationPending 設定為 true。 在這種情況下,Cancelled你的RunWorkerCompleted事件處理器中的 旗System.ComponentModel.RunWorkerCompletedEventArgs標不會被設為 true,儘管已經提出取消請求。 這種情況稱為 競賽條件 ,是多執行緒程式設計中常見的問題。 欲了解更多多執行緒設計問題,請參閱管理執行任務最佳實務。

如果你的操作產生了結果,你可以將該結果指派給該 DoWorkEventArgs.Result 屬性。 這些資料將提供給 RunWorkerCompleted 物業內 RunWorkerCompletedEventArgs.Result 的事件處理人員。

如果操作產生了你的程式碼無法處理的例外,它BackgroundWorker會捕捉該異常並傳入事件處理程序RunWorkerCompleted,並以 的System.ComponentModel.RunWorkerCompletedEventArgs屬性形式揭露Error。 如果你是在 Visual Studio 除錯器下執行,除錯器會在事件處理程序中未處理異常被觸發的那個點 DoWork 出問題。 如果你有多個 BackgroundWorker,就不應該直接參考其中任何一個,因為這會讓你的事件處理器與 的特定BackgroundWorker實例 連結DoWork。 相反地,你應該透過在事件處理程序中DoWork投射參數sender來存取你的BackgroundWorker

你必須小心,不要在事件 DoWork 處理程序中操作任何使用者介面物件。 相反地,應該透過 BackgroundWorker 事件與使用者介面溝通。

欲了解更多如何處理事件的資訊,請參閱 「處理與提升事件」。

適用於

另請參閱