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.
private 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 。 這種情況稱為 競爭條件 ,在多執行緒程式設計中是常見的考慮。 如需多執行緒設計問題的詳細資訊,請參閱 Managed 執行緒最佳做法

如果您的作業產生結果,您可以將結果指派給 DoWorkEventArgs.Result 屬性。 這將會提供給 RunWorkerCompleted 屬性中的 RunWorkerCompletedEventArgs.Result 事件處理常式使用。

如果作業引發程式碼未處理的例外狀況,則會 BackgroundWorker 攔截例外狀況,並將其傳遞至 RunWorkerCompleted 事件處理常式,其中會公開為 ErrorSystem.ComponentModel.RunWorkerCompletedEventArgs 屬性。 如果您在 Visual Studio 偵錯工具下執行,偵錯工具將會在引發未處理例外狀況的事件處理常式中 DoWork 中斷。 如果您有多個 BackgroundWorker ,則不應該直接參考其中任何一個,因為這樣會將事件處理常式結合 DoWork 至 特定的 實例 BackgroundWorker 。 相反地,您應該在事件處理常式中 DoWork 轉換 參數來 sender 存取 BackgroundWorker

您必須小心不要操作事件處理常式中的任何 DoWork 使用者介面物件。 相反地,透過 BackgroundWorker 事件與使用者介面通訊。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱