BackgroundWorker.RunWorkerCompleted イベント

定義

バックグラウンド操作の完了時、キャンセル時、またはバックグラウンド操作によって例外が発生したときに発生します。Occurs when the background operation has completed, has been canceled, or has raised an exception.

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 イベントを使用して、非同期操作の結果を処理する方法を示しています。The following code example demonstrates the use of the RunWorkerCompleted event to handle the result of an asynchronous operation. このコード例は、BackgroundWorker クラス用に用意されている大規模な例の一部です。This code example is part of a larger example provided for the BackgroundWorker class.

// 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 イベントハンドラーがを返したときに発生します。This event is raised when the DoWork event handler returns.

操作が正常に完了し、その結果が DoWork イベントハンドラーに割り当てられている場合は、RunWorkerCompletedEventArgs.Result プロパティを使用して結果にアクセスできます。If the operation completes successfully and its result is assigned in the DoWork event handler, you can access the result through the RunWorkerCompletedEventArgs.Result property.

System.ComponentModel.RunWorkerCompletedEventArgsError プロパティは、操作によって例外がスローされたことを示します。The Error property of System.ComponentModel.RunWorkerCompletedEventArgs indicates that an exception was thrown by the operation.

System.ComponentModel.RunWorkerCompletedEventArgsCancelled プロパティは、キャンセル要求がバックグラウンド操作によって処理されたかどうかを示します。The Cancelled property of System.ComponentModel.RunWorkerCompletedEventArgs indicates whether a cancellation request was processed by the background operation. DoWork イベントハンドラーのコードが CancellationPending フラグをチェックし、System.ComponentModel.DoWorkEventArgsCancel フラグを trueに設定することによってキャンセル要求を検出すると、CancelledSystem.ComponentModel.RunWorkerCompletedEventArgs フラグも trueに設定されます。If your code in the DoWork event handler detects a cancellation request by checking the CancellationPending flag and setting the Cancel flag of System.ComponentModel.DoWorkEventArgs to true, the Cancelled flag of System.ComponentModel.RunWorkerCompletedEventArgs also will be set to true.

注意事項

キャンセル要求の実行中に、DoWork イベントハンドラーのコードが作業を終了する可能性があることに注意してください。ポーリングループが CancellationPending trueに設定されている可能性があります。Be aware that your code in the DoWork event handler may finish its work as a cancellation request is being made, and your polling loop may miss CancellationPending being set to true. この場合、キャンセル要求が行われた場合でも、RunWorkerCompleted イベントハンドラーの System.ComponentModel.RunWorkerCompletedEventArgsCancelled フラグは trueに設定されません。In this case, the Cancelled flag of System.ComponentModel.RunWorkerCompletedEventArgs in your RunWorkerCompleted event handler will not be set to true, even though a cancellation request was made. この状況は競合状態と呼ばれ、マルチスレッドプログラミングの一般的な問題です。This situation is called a race condition and is a common concern in multithreaded programming. マルチスレッドのデザインの問題の詳細については、「マネージスレッド処理のベストプラクティス」を参照してください。For more information about multithreading design issues, see Managed Threading Best Practices.

RunWorkerCompleted イベントハンドラーは、RunWorkerCompletedEventArgs.Result プロパティにアクセスする前に、常に AsyncCompletedEventArgs.ErrorAsyncCompletedEventArgs.Cancelled プロパティを確認する必要があります。Your RunWorkerCompleted event handler should always check the AsyncCompletedEventArgs.Error and AsyncCompletedEventArgs.Cancelled properties before accessing the RunWorkerCompletedEventArgs.Result property. 例外が発生した場合、または操作が取り消された場合は、RunWorkerCompletedEventArgs.Result プロパティにアクセスすると例外が発生します。If an exception was raised or if the operation was canceled, accessing the RunWorkerCompletedEventArgs.Result property raises an exception.

適用対象

こちらもご覧ください