Bagikan melalui


BackgroundWorker.RunWorkerCompleted Kejadian

Definisi

Terjadi ketika operasi latar belakang telah selesai, telah dibatalkan, atau telah memunculkan pengecualian.

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 

Jenis Acara

Contoh

Contoh kode berikut menunjukkan penggunaan RunWorkerCompleted peristiwa untuk menangani hasil operasi asinkron. Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk BackgroundWorker kelas .

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

Keterangan

Kejadian ini dimunculkan saat penanganan DoWork aktivitas kembali.

Jika operasi berhasil diselesaikan dan hasilnya ditetapkan di penanganan DoWork aktivitas, Anda dapat mengakses hasilnya melalui RunWorkerCompletedEventArgs.Result properti .

Properti Error dari System.ComponentModel.RunWorkerCompletedEventArgs menunjukkan bahwa pengecualian dilemparkan oleh operasi.

Properti Cancelled dari System.ComponentModel.RunWorkerCompletedEventArgs menunjukkan apakah permintaan pembatalan diproses oleh operasi latar belakang. Jika kode Anda di DoWork penanganan aktivitas mendeteksi permintaan pembatalan dengan memeriksa CancellationPending bendera dan mengatur Cancel bendera System.ComponentModel.DoWorkEventArgs ke true, Cancelled bendera System.ComponentModel.RunWorkerCompletedEventArgs juga akan diatur ke true.

Perhatian

Ketahuilah bahwa kode Anda di DoWork penanganan aktivitas dapat menyelesaikan pekerjaannya karena permintaan pembatalan sedang dibuat, dan perulangan polling Anda mungkin melewatkan CancellationPending diatur ke true. Dalam hal ini, Cancelled bendera System.ComponentModel.RunWorkerCompletedEventArgs di penanganan aktivitas Anda RunWorkerCompleted tidak akan diatur ke true, meskipun permintaan pembatalan dibuat. Situasi ini disebut kondisi balapan dan menjadi perhatian umum dalam pemrograman multithreaded. Untuk informasi selengkapnya tentang masalah desain multithreading, lihat Praktik Terbaik Utas Terkelola.

Penanganan aktivitas Anda RunWorkerCompleted harus selalu memeriksa AsyncCompletedEventArgs.Error properti dan AsyncCompletedEventArgs.Cancelled sebelum mengakses RunWorkerCompletedEventArgs.Result properti . Jika pengecualian dinaikkan atau jika operasi dibatalkan, mengakses RunWorkerCompletedEventArgs.Result properti akan menimbulkan pengecualian.

Berlaku untuk

Lihat juga