BackgroundWorker.RunWorkerCompleted 事件

定義

當背景作業已完成、取消或引發例外狀況時發生。

public:
 event System::ComponentModel::RunWorkerCompletedEventHandler ^ RunWorkerCompleted;
public event System.ComponentModel.RunWorkerCompletedEventHandler RunWorkerCompleted;
public event System.ComponentModel.RunWorkerCompletedEventHandler? RunWorkerCompleted;
member this.RunWorkerCompleted : System.ComponentModel.RunWorkerCompletedEventHandler 
Public Custom Event RunWorkerCompleted As RunWorkerCompletedEventHandler 

事件類型

範例

下列程式代碼範例示範如何使用 RunWorkerCompleted 事件來處理異步操作的結果。 此程式代碼範例是針對 類別提供的較大範例的 BackgroundWorker 一部分。

// This event handler deals with the results of the
// background operation.
void backgroundWorker1_RunWorkerCompleted( Object^ /*sender*/, RunWorkerCompletedEventArgs^ e )
{
   // First, handle the case where an exception was thrown.
   if ( e->Error != nullptr )
   {
      MessageBox::Show( e->Error->Message );
   }
   else
   if ( e->Cancelled )
   {
      // Next, handle the case where the user cancelled 
      // the operation.
      // Note that due to a race condition in 
      // the DoWork event handler, the Cancelled
      // flag may not have been set, even though
      // CancelAsync was called.
      resultLabel->Text = "Cancelled";
   }
   else
   {
      // Finally, handle the case where the operation 
      // succeeded.
      resultLabel->Text = e->Result->ToString();
   }

   // Enable the UpDown control.
   this->numericUpDown1->Enabled = true;

   // Enable the Start button.
   startAsyncButton->Enabled = true;

   // Disable the Cancel button.
   cancelAsyncButton->Enabled = false;
}
// This event handler deals with the results of the
// background operation.
private void backgroundWorker1_RunWorkerCompleted(
    object sender, RunWorkerCompletedEventArgs e)
{
    // First, handle the case where an exception was thrown.
    if (e.Error != null)
    {
        MessageBox.Show(e.Error.Message);
    }
    else if (e.Cancelled)
    {
        // Next, handle the case where the user canceled 
        // the operation.
        // Note that due to a race condition in 
        // the DoWork event handler, the Cancelled
        // flag may not have been set, even though
        // CancelAsync was called.
        resultLabel.Text = "Canceled";
    }
    else
    {
        // Finally, handle the case where the operation 
        // succeeded.
        resultLabel.Text = e.Result.ToString();
    }

    // Enable the UpDown control.
    this.numericUpDown1.Enabled = true;

    // Enable the Start button.
    startAsyncButton.Enabled = true;

    // Disable the Cancel button.
    cancelAsyncButton.Enabled = false;
}
' This event handler deals with the results of the
' background operation.
Private Sub backgroundWorker1_RunWorkerCompleted( _
ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
Handles backgroundWorker1.RunWorkerCompleted

    ' First, handle the case where an exception was thrown.
    If (e.Error IsNot Nothing) Then
        MessageBox.Show(e.Error.Message)
    ElseIf e.Cancelled Then
        ' Next, handle the case where the user canceled the 
        ' operation.
        ' Note that due to a race condition in 
        ' the DoWork event handler, the Cancelled
        ' flag may not have been set, even though
        ' CancelAsync was called.
        resultLabel.Text = "Canceled"
    Else
        ' Finally, handle the case where the operation succeeded.
        resultLabel.Text = e.Result.ToString()
    End If

    ' Enable the UpDown control.
    Me.numericUpDown1.Enabled = True

    ' Enable the Start button.
    startAsyncButton.Enabled = True

    ' Disable the Cancel button.
    cancelAsyncButton.Enabled = False
End Sub

備註

當事件處理程式傳回時, DoWork 就會引發這個事件。

如果作業順利完成,並在事件處理程式中 DoWork 指派其結果,您可以透過 RunWorkerCompletedEventArgs.Result 屬性存取結果。

Error 屬性 System.ComponentModel.RunWorkerCompletedEventArgs 表示作業擲回例外狀況。

Cancelled 屬性 System.ComponentModel.RunWorkerCompletedEventArgs 會指出背景作業是否處理取消要求。 勾選您的事件處理程式中的DoWork程式碼藉由檢查 CancellationPending 旗標並將 的旗trueSystem.ComponentModel.DoWorkEventArgs標設定Cancel為 來偵測取消要求,Cancelled則 的System.ComponentModel.RunWorkerCompletedEventArgs旗標也會設定為 true

警告

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

您的RunWorkerCompleted事件處理程式應該一律先檢查 和 AsyncCompletedEventArgs.Cancelled 屬性,AsyncCompletedEventArgs.Error再存取 RunWorkerCompletedEventArgs.Result 屬性。 如果引發例外狀況,或作業已取消,則存取 RunWorkerCompletedEventArgs.Result 屬性會引發例外狀況。

適用於

另請參閱