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事件處理程式中的 RunWorkerCompletedSystem.ComponentModel.RunWorkerCompletedEventArgs標將會設定為 trueSystem.ComponentModel.DoWorkEventArgs

警告

請注意,事件處理程式中的 DoWork 程式代碼可能會在發出取消要求時完成其工作,而您的輪詢迴圈可能會遺漏 CancellationPending 設定為 true。 在這裡情況下,Cancelled即使已提出取消要求,事件處理程式中的 RunWorkerCompletedSystem.ComponentModel.RunWorkerCompletedEventArgs標也不會設定為 true。 這種情況稱為 競爭條件 ,而且是多線程程式設計中常見的考慮。 如需多線程設計問題的詳細資訊,請參閱 受控線程最佳做法

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

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

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

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

適用於

另請參閱