BackgroundWorker.RunWorkerCompleted BackgroundWorker.RunWorkerCompleted BackgroundWorker.RunWorkerCompleted BackgroundWorker.RunWorkerCompleted Event

정의

백그라운드 작업이 완료되거나 취소되거나 예외를 발생시켰을 때 발생합니다.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 'backgroundWorker1_RunWorkerCompleted

설명

이 이벤트는 때를 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.

합니다 Error 속성의 System.ComponentModel.RunWorkerCompletedEventArgs 작업에서 예외가 발생을 나타냅니다.The Error property of System.ComponentModel.RunWorkerCompletedEventArgs indicates that an exception was thrown by the operation.

합니다 Cancelled 속성의 System.ComponentModel.RunWorkerCompletedEventArgs 취소 요청을 백그라운드 작업에서 처리 된 여부를 나타냅니다.The Cancelled property of System.ComponentModel.RunWorkerCompletedEventArgs indicates whether a cancellation request was processed by the background operation. 경우에서 코드를 DoWork 이벤트 처리기를 확인 하 여 취소 요청을 감지 합니다 CancellationPending 플래그 및 설정을 Cancel 의 플래그 System.ComponentModel.DoWorkEventArgstrue, Cancelled 의 플래그 System.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. 이 경우에 Cancelled 의 플래그 System.ComponentModel.RunWorkerCompletedEventArgs 에서 RunWorkerCompleted 이벤트 처리기 설정 하지 것입니다 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. 다중 스레드 디자인에 대 한 자세한 정보에 대 한 문제를 참조 하세요 Managed Threading Best Practices합니다.For more information about multithreading design issues, see Managed Threading Best Practices.

프로그램 RunWorkerCompleted 이벤트 처리기를 항상 확인 해야 합니다 AsyncCompletedEventArgs.ErrorAsyncCompletedEventArgs.Cancelled 속성에 액세스 하기 전에 RunWorkerCompletedEventArgs.Result 속성.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.

적용 대상

추가 정보