Aracılığıyla paylaş


BackgroundWorker.RunWorkerCompleted Olay

Tanım

Arka plan işlemi tamamlandığında, iptal edildiğinde veya bir özel durum oluştuğunda gerçekleşir.

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 

Olay Türü

Örnekler

Aşağıdaki kod örneği, zaman uyumsuz bir işlemin sonucunu işlemek için olayının kullanımını RunWorkerCompleted gösterir. Bu kod örneği, sınıfı için BackgroundWorker sağlanan daha büyük bir örneğin parçasıdır.

// 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

Açıklamalar

Olay işleyicisi döndürdüğünde DoWork bu olay oluşturulur.

İşlem başarıyla tamamlanırsa ve sonucu olay işleyicisinde DoWork atanırsa, sonuca özelliği aracılığıyla RunWorkerCompletedEventArgs.Result erişebilirsiniz.

Error özelliğiSystem.ComponentModel.RunWorkerCompletedEventArgs, işlem tarafından bir özel durum oluşturulduğuna işaret eder.

Cancelled özelliğiSystem.ComponentModel.RunWorkerCompletedEventArgs, bir iptal isteğinin arka plan işlemi tarafından işlenip işlenmediğini gösterir. Olay işleyicisindeki DoWork kodunuz bayrağını denetleyerek CancellationPending ve bayrağını trueSystem.ComponentModel.DoWorkEventArgs olarak ayarlayarak Cancel bir iptal isteği algılarsa bayrağı CancelledSystem.ComponentModel.RunWorkerCompletedEventArgs da olarak trueayarlanır.

Dikkat

Olay işleyicisindeki DoWork kodunuzun bir iptal isteği yapılırken çalışmasını tamamlayaabileceğini ve yoklama döngünüzün olarak ayarlanmasını truekaçırabileceğini CancellationPending unutmayın. Bu durumda, Cancelled bir iptal isteği yapılmış olsa bile olay işleyicinizdeki RunWorkerCompleted bayrağı System.ComponentModel.RunWorkerCompletedEventArgs olarak ayarlanmaztrue. Bu durum bir yarış durumu olarak adlandırılır ve çok iş parçacıklı programlamada yaygın bir sorundur. Çok iş parçacıklı tasarım sorunları hakkında daha fazla bilgi için bkz. Yönetilen İş Parçacığı Oluşturma En İyi Yöntemleri.

Olay işleyicinizRunWorkerCompleted, özelliğe erişmeden RunWorkerCompletedEventArgs.Result önce her zaman ve AsyncCompletedEventArgs.Cancelled özelliklerini denetlemelidirAsyncCompletedEventArgs.Error. Bir özel durum oluşturulduysa veya işlem iptal edildiyse, özelliğe erişmek RunWorkerCompletedEventArgs.Result bir özel durum oluşturur.

Şunlara uygulanır

Ayrıca bkz.