BackgroundWorker.RunWorkerAsync Método

Definição

Inicia a execução de uma operação em segundo plano.

Sobrecargas

RunWorkerAsync()

Inicia a execução de uma operação em segundo plano.

RunWorkerAsync(Object)

Inicia a execução de uma operação em segundo plano.

RunWorkerAsync()

Origem:
BackgroundWorker.cs
Origem:
BackgroundWorker.cs
Origem:
BackgroundWorker.cs

Inicia a execução de uma operação em segundo plano.

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

Exceções

Exemplos

O exemplo de código a seguir demonstra o uso do RunWorkerAsync método para iniciar uma operação assíncrona. Ele faz parte de um exemplo maior descrito em Como baixar um arquivo em segundo plano.

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

Comentários

O RunWorkerAsync método envia uma solicitação para iniciar a operação em execução de forma assíncrona. Quando a solicitação é atendida, o DoWork evento é acionado, o que, por sua vez, inicia a execução da operação em segundo plano.

Se a operação em segundo plano já estiver em execução, chamar RunWorkerAsync novamente gerará um InvalidOperationException.

Confira também

Aplica-se a

RunWorkerAsync(Object)

Origem:
BackgroundWorker.cs
Origem:
BackgroundWorker.cs
Origem:
BackgroundWorker.cs

Inicia a execução de uma operação em segundo plano.

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)

Parâmetros

argument
Object

Um parâmetro para uso pela operação em segundo plano a ser executado no manipulador de eventos DoWork.

Exceções

Exemplos

O exemplo de código a seguir demonstra o uso do RunWorkerAsync método para iniciar uma operação assíncrona. Este exemplo de código faz parte de um exemplo maior fornecido para a BackgroundWorker classe .

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

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

Comentários

O RunWorkerAsync método envia uma solicitação para iniciar a operação em execução de forma assíncrona. Quando a solicitação é atendida, o DoWork evento é acionado, o que, por sua vez, inicia a execução da operação em segundo plano.

Se a operação exigir um parâmetro, você poderá fornecê-lo como o argument parâmetro para RunWorkerAsync.

Se a operação em segundo plano já estiver em execução, chamar RunWorkerAsync novamente gerará um InvalidOperationException.

Confira também

Aplica-se a