Bagikan melalui


BackgroundWorker.RunWorkerAsync Metode

Definisi

Memulai eksekusi operasi latar belakang.

Overload

RunWorkerAsync()

Memulai eksekusi operasi latar belakang.

RunWorkerAsync(Object)

Memulai eksekusi operasi latar belakang.

RunWorkerAsync()

Sumber:
BackgroundWorker.cs
Sumber:
BackgroundWorker.cs
Sumber:
BackgroundWorker.cs

Memulai eksekusi operasi latar belakang.

public:
 void RunWorkerAsync();
public void RunWorkerAsync ();
member this.RunWorkerAsync : unit -> unit
Public Sub RunWorkerAsync ()

Pengecualian

Contoh

Contoh kode berikut menunjukkan penggunaan RunWorkerAsync metode untuk memulai operasi asinkron. Ini adalah bagian dari contoh yang lebih besar yang dijelaskan dalam Cara: Mengunduh File di Latar Belakang.

private void downloadButton_Click(object sender, EventArgs e)
{
    // Start the download operation in the background.
    this.backgroundWorker1.RunWorkerAsync();

    // Disable the button for the duration of the download.
    this.downloadButton.Enabled = false;

    // Once you have started the background thread you 
    // can exit the handler and the application will 
    // wait until the RunWorkerCompleted event is raised.

    // Or if you want to do something else in the main thread,
    // such as update a progress bar, you can do so in a loop 
    // while checking IsBusy to see if the background task is
    // still running.

    while (this.backgroundWorker1.IsBusy)
    {
        progressBar1.Increment(1);
        // Keep UI messages moving, so the form remains 
        // responsive during the asynchronous operation.
        Application.DoEvents();
    }
}
Private Sub downloadButton_Click( _
    ByVal sender As Object, _
    ByVal e As EventArgs) _
    Handles downloadButton.Click

    ' Start the download operation in the background.
    Me.backgroundWorker1.RunWorkerAsync()

    ' Disable the button for the duration of the download.
    Me.downloadButton.Enabled = False

    ' Once you have started the background thread you 
    ' can exit the handler and the application will 
    ' wait until the RunWorkerCompleted event is raised.

    ' If you want to do something else in the main thread,
    ' such as update a progress bar, you can do so in a loop 
    ' while checking IsBusy to see if the background task is
    ' still running.
    While Me.backgroundWorker1.IsBusy
        progressBar1.Increment(1)
        ' Keep UI messages moving, so the form remains 
        ' responsive during the asynchronous operation.
        Application.DoEvents()
    End While
End Sub

Keterangan

Metode RunWorkerAsync mengirimkan permintaan untuk memulai operasi yang berjalan secara asinkron. Ketika permintaan dilayanakan, DoWork peristiwa dinaikkan, yang pada gilirannya memulai eksekusi operasi latar belakang Anda.

Jika operasi latar belakang sudah berjalan, panggilan RunWorkerAsync lagi akan menaikkan InvalidOperationException.

Lihat juga

Berlaku untuk

RunWorkerAsync(Object)

Sumber:
BackgroundWorker.cs
Sumber:
BackgroundWorker.cs
Sumber:
BackgroundWorker.cs

Memulai eksekusi operasi latar belakang.

public:
 void RunWorkerAsync(System::Object ^ argument);
public void RunWorkerAsync (object argument);
public void RunWorkerAsync (object? argument);
member this.RunWorkerAsync : obj -> unit
Public Sub RunWorkerAsync (argument As Object)

Parameter

argument
Object

Parameter untuk digunakan oleh operasi latar belakang yang akan dijalankan di penanganan DoWork aktivitas.

Pengecualian

Contoh

Contoh kode berikut menunjukkan penggunaan RunWorkerAsync metode untuk memulai operasi asinkron. Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk BackgroundWorker kelas .

// Start the asynchronous operation.
backgroundWorker1->RunWorkerAsync( numberToCompute );
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync(numberToCompute);

' Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync(numberToCompute)

Keterangan

Metode RunWorkerAsync mengirimkan permintaan untuk memulai operasi yang berjalan secara asinkron. Ketika permintaan dilayanakan, DoWork peristiwa dinaikkan, yang pada gilirannya memulai eksekusi operasi latar belakang Anda.

Jika operasi Anda memerlukan parameter, Anda dapat menyediakannya sebagai argument parameter ke RunWorkerAsync.

Jika operasi latar belakang sudah berjalan, panggilan RunWorkerAsync lagi akan menaikkan InvalidOperationException.

Lihat juga

Berlaku untuk