Share via


BackgroundWorker.RunWorkerAsync Metoda

Definice

Spustí operaci na pozadí.

Přetížení

RunWorkerAsync()

Spustí operaci na pozadí.

RunWorkerAsync(Object)

Spustí operaci na pozadí.

RunWorkerAsync()

Zdroj:
BackgroundWorker.cs
Zdroj:
BackgroundWorker.cs
Zdroj:
BackgroundWorker.cs

Spustí operaci na pozadí.

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

Výjimky

Příklady

Následující příklad kódu ukazuje použití RunWorkerAsync metody ke spuštění asynchronní operace. Je součástí rozsáhlejšího příkladu popsaného v tématu Postupy: Stažení souboru na pozadí.

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

Poznámky

Metoda RunWorkerAsync odešle požadavek na spuštění operace spuštěné asynchronně. Při zpracování DoWork požadavku se vyvolá událost, která následně spustí operaci na pozadí.

Pokud už je operace na pozadí spuštěná, vyvolá se opětovným voláním RunWorkerAsyncInvalidOperationException.

Viz také

Platí pro

RunWorkerAsync(Object)

Zdroj:
BackgroundWorker.cs
Zdroj:
BackgroundWorker.cs
Zdroj:
BackgroundWorker.cs

Spustí operaci na pozadí.

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)

Parametry

argument
Object

Parametr pro použití operací na pozadí, která má být provedena v obslužné rutině DoWork události.

Výjimky

Příklady

Následující příklad kódu ukazuje použití RunWorkerAsync metody ke spuštění asynchronní operace. Tento příklad kódu je součástí většího příkladu BackgroundWorker pro třídu .

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

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

Poznámky

Metoda RunWorkerAsync odešle požadavek na spuštění operace spuštěné asynchronně. Při zpracování DoWork požadavku se vyvolá událost, která následně spustí operaci na pozadí.

Pokud vaše operace vyžaduje parametr, můžete ho zadat jako argument parametr do RunWorkerAsync.

Pokud už je operace na pozadí spuštěná, vyvolá se opětovným voláním RunWorkerAsyncInvalidOperationException.

Viz také

Platí pro