BackgroundWorker.RunWorkerAsync Méthode

Définition

Démarre l’exécution d’une opération en arrière-plan.

Surcharges

Nom Description
RunWorkerAsync()

Démarre l’exécution d’une opération en arrière-plan.

RunWorkerAsync(Object)

Démarre l’exécution d’une opération en arrière-plan.

RunWorkerAsync()

Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs

Démarre l’exécution d’une opération en arrière-plan.

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

Exceptions

IsBusy a la valeur true.

Exemples

L’exemple de code suivant illustre l’utilisation de la RunWorkerAsync méthode pour démarrer une opération asynchrone. Il fait partie d’un exemple plus large décrit dans How to : Download a File in the Background.

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

    // Disable the button for the duration of the download.
    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 (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

Remarques

La RunWorkerAsync méthode envoie une demande pour démarrer l’opération en cours d’exécution de manière asynchrone. Lorsque la demande est serviceée, l’événement DoWork est déclenché, ce qui démarre à son tour l’exécution de votre opération en arrière-plan.

Si l’opération en arrière-plan est déjà en cours d’exécution, l’appel RunWorkerAsync à nouveau déclenche un InvalidOperationException.

Voir aussi

S’applique à

RunWorkerAsync(Object)

Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs

Démarre l’exécution d’une opération en arrière-plan.

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)

Paramètres

argument
Object

Paramètre à utiliser par l’opération en arrière-plan à exécuter dans le DoWork gestionnaire d’événements.

Exceptions

IsBusy a la valeur true.

Exemples

L’exemple de code suivant illustre l’utilisation de la RunWorkerAsync méthode pour démarrer une opération asynchrone. Cet exemple de code fait partie d’un exemple plus grand fourni pour la BackgroundWorker classe.

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

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

Remarques

La RunWorkerAsync méthode envoie une demande pour démarrer l’opération en cours d’exécution de manière asynchrone. Lorsque la demande est serviceée, l’événement DoWork est déclenché, ce qui démarre à son tour l’exécution de votre opération en arrière-plan.

Si votre opération nécessite un paramètre, vous pouvez le fournir en tant que argument paramètre .RunWorkerAsync

Si l’opération en arrière-plan est déjà en cours d’exécution, l’appel RunWorkerAsync à nouveau déclenche un InvalidOperationException.

Voir aussi

S’applique à