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 アクセスできます。

System.ComponentModel.RunWorkerCompletedEventArgs プロパティはError、操作によって例外がスローされたことを示します。

System.ComponentModel.RunWorkerCompletedEventArgs プロパティはCancelled、キャンセル要求がバックグラウンド操作によって処理されたかどうかを示します。 イベント ハンドラーのコードでDoWork、フラグをチェックし、 のフラグを CancellationPendingCancelledtrue設定してキャンセル要求をCancelSystem.ComponentModel.DoWorkEventArgs検出した場合、 のSystem.ComponentModel.RunWorkerCompletedEventArgsフラグも にtrue設定されます。

注意事項

イベント ハンドラーのコードは、取り消し要求が行われているのでDoWork作業を終了する可能性があり、ポーリング ループが にtrue設定されていない可能性があることにCancellationPending注意してください。 この場合、キャンセル要求がCancelled行われた場合でも、イベント ハンドラー内RunWorkerCompletedの の フラグSystem.ComponentModel.RunWorkerCompletedEventArgsは にtrue設定されません。 この状況は 競合状態 と呼ばれ、マルチスレッド プログラミングでは一般的な懸念事項です。 マルチスレッド設計の問題の詳細については、「 マネージド スレッドのベスト プラクティス」を参照してください。

RunWorkerCompletedイベント ハンドラーは、 プロパティにアクセスする前に、 プロパティと AsyncCompletedEventArgs.Cancelled プロパティを常にチェックAsyncCompletedEventArgs.ErrorするRunWorkerCompletedEventArgs.Result必要があります。 例外が発生した場合、または操作が取り消された場合、 プロパティに RunWorkerCompletedEventArgs.Result アクセスすると例外が発生します。

適用対象

こちらもご覧ください