次の方法で共有


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.
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.
    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.
    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.RunWorkerCompletedEventArgsError プロパティは、操作によって例外がスローされたことを示します。

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

注意事項

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

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

適用対象

こちらもご覧ください