Share via


RunWorkerCompletedEventArgs Kelas

Definisi

Menyediakan data untuk peristiwa MethodNameCompleted .

public ref class RunWorkerCompletedEventArgs : System::ComponentModel::AsyncCompletedEventArgs
public class RunWorkerCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
type RunWorkerCompletedEventArgs = class
    inherit AsyncCompletedEventArgs
Public Class RunWorkerCompletedEventArgs
Inherits AsyncCompletedEventArgs
Warisan
RunWorkerCompletedEventArgs

Contoh

Contoh kode berikut menunjukkan penggunaan RunWorkerCompletedEventArgs. Contoh ini adalah bagian dari sampel yang lebih besar 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

Saat menggunakan pola asinkron berbasis peristiwa untuk operasi asinkron, formulir atau kontrol Formulir Windows memulai operasi asinkron dengan memanggil BackgroundWorker.RunWorkerAsync metode . Metode pada gilirannya BackgroundWorker.DoWork meningkatkan peristiwa secara asinkron dan meneruskannya sebagai DoWorkEventArgs instans. Jika operasi asinkron mengembalikan nilai, penanganan BackgroundWorker.DoWork aktivitas biasanya menetapkannya ke DoWorkEventArgs.Result properti . Ketika operasi asinkron selesai, BackgroundWorker.RunWorkerCompleted peristiwa dinaikkan dan diteruskan RunWorkerCompletedEventArgs instans yang berisi informasi tentang status operasi (apakah itu dibatalkan, disalahkan, atau berhasil diselesaikan). Jika berhasil diselesaikan, propertinya Result berisi nilai yang dikembalikan oleh operasi asinkron dan sebelumnya ditetapkan ke DoWorkEventArgs.Result properti .

Catatan

Atribut HostProtectionAttribute yang diterapkan ke kelas ini memiliki nilai properti berikut Resources : SharedState. HostProtectionAttribute tidak memengaruhi aplikasi desktop (yang biasanya dimulai dengan mengklik dua kali ikon, mengetik perintah, atau memasukkan URL di browser). Untuk informasi selengkapnya, lihat HostProtectionAttribute kelas atau SQL Server Atribut Pemrograman dan Perlindungan Host.

Konstruktor

RunWorkerCompletedEventArgs(Object, Exception, Boolean)

Menginisialisasi instans baru kelas RunWorkerCompletedEventArgs.

Properti

Cancelled

Mendapatkan nilai yang menunjukkan apakah operasi asinkron telah dibatalkan.

(Diperoleh dari AsyncCompletedEventArgs)
Error

Mendapatkan nilai yang menunjukkan kesalahan mana yang terjadi selama operasi asinkron.

(Diperoleh dari AsyncCompletedEventArgs)
Result

Mendapatkan nilai yang mewakili hasil operasi asinkron.

UserState

Mendapatkan nilai yang mewakili status pengguna.

Metode

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan dari instans Type saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari saat ini Object.

(Diperoleh dari Object)
RaiseExceptionIfNecessary()

Memunculkan pengecualian yang disediakan pengguna jika operasi asinkron gagal.

(Diperoleh dari AsyncCompletedEventArgs)
ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Berlaku untuk

Lihat juga