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 작업에 의해 예외가 throw되었음을 나타냅니다.

System.ComponentModel.RunWorkerCompletedEventArgs 속성은 Cancelled 취소 요청이 백그라운드 작업에서 처리되었는지 여부를 나타냅니다. 이벤트 처리기의 코드가 DoWork 플래그를 확인하고 플래그를 CancellationPending 로 설정 System.ComponentModel.DoWorkEventArgsCancel 하여 취소 요청을 검색하는 trueCancelled 경우 의 System.ComponentModel.RunWorkerCompletedEventArgs 플래그도 로 true설정됩니다.

주의

이벤트 처리기의 코드 DoWork 는 취소 요청이 수행될 때 해당 작업을 완료할 수 있으며 폴링 루프가 로 true설정되지 않을 CancellationPending 수 있습니다. 이 경우 취소 요청이 Cancelled 발생하더라도 이벤트 처리기의 플래그 System.ComponentModel.RunWorkerCompletedEventArgsRunWorkerCompleted 가 로 true설정되지 않습니다. 이 상황을 경합 상태 라고 하며 다중 스레드 프로그래밍의 일반적인 관심사입니다. 다중 스레딩 디자인 문제에 대한 자세한 내용은 관리되는 스레딩 모범 사례를 참조하세요.

이벤트 RunWorkerCompleted 처리기는 속성에 AsyncCompletedEventArgs.Error 액세스 RunWorkerCompletedEventArgs.Result 하기 전에 항상 및 AsyncCompletedEventArgs.Cancelled 속성을 검사 합니다. 예외가 발생하거나 작업이 취소된 경우 속성에 RunWorkerCompletedEventArgs.Result 액세스하면 예외가 발생합니다.

적용 대상

추가 정보