Procédure pas à pas : exécution d'une opération en arrière-plan

Si vous avez une opération qui prendra un certain temps et que vous ne souhaitez pas causer de retards dans votre interface utilisateur, vous pouvez utiliser la classe BackgroundWorker pour exécuter l'opération sur un autre thread.

Pour obtenir la liste complète du code utilisé dans cet exemple, consultez Guide pratique pour exécuter une opération en arrière-plan.

Exécuter une opération en arrière-plan

  1. Avec votre formulaire actif dans le Concepteur Windows Forms dans Visual Studio, faites glisser deux Button contrôles de la boîte à outils vers le formulaire, puis définissez les propriétés et Text les Name propriétés des boutons en fonction du tableau suivant.

    Bouton Nom Text
    button1 startBtn Démarrer
    button2 cancelBtn Annuler
  2. Ouvrez la boîte à outils, cliquez sur l’onglet Composants , puis faites glisser le BackgroundWorker composant sur votre formulaire.

    Le backgroundWorker1 composant apparaît dans la barre d’état du composant.

  3. Dans la fenêtre Propriétés , définissez la propriété WorkerSupportsCancellation sur true.

  4. Dans la fenêtre Propriétés , cliquez sur le bouton Événements , puis double-cliquez sur les DoWork événements et RunWorkerCompleted les événements pour créer des gestionnaires d’événements.

  5. Insérez votre code fastidieux dans le gestionnaire d’événements DoWork .

  6. Extrayez tous les paramètres requis par l’opération à partir de la Argument propriété du DoWorkEventArgs paramètre.

  7. Affectez le résultat du calcul à la Result propriété du DoWorkEventArgs.

    Il sera disponible pour le RunWorkerCompleted gestionnaire d’événements.

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // Do not access the form's BackgroundWorker reference directly.
        // Instead, use the reference provided by the sender parameter.
        BackgroundWorker bw = sender as BackgroundWorker;
    
        // Extract the argument.
        int arg = (int)e.Argument;
    
        // Start the time-consuming operation.
        e.Result = TimeConsumingOperation(bw, arg);
    
        // If the operation was canceled by the user,
        // set the DoWorkEventArgs.Cancel property to true.
        if (bw.CancellationPending)
        {
            e.Cancel = true;
        }
    }
    
    Private Sub backgroundWorker1_DoWork( _
    sender As Object, e As DoWorkEventArgs) _
    Handles backgroundWorker1.DoWork
    
       ' Do not access the form's BackgroundWorker reference directly.
       ' Instead, use the reference provided by the sender parameter.
       Dim bw As BackgroundWorker = CType( sender, BackgroundWorker )
       
       ' Extract the argument.
       Dim arg As Integer = Fix(e.Argument)
       
       ' Start the time-consuming operation.
       e.Result = TimeConsumingOperation(bw, arg)
       
       ' If the operation was canceled by the user, 
       ' set the DoWorkEventArgs.Cancel property to true.
       If bw.CancellationPending Then
          e.Cancel = True
       End If
    
    End Sub   
    
  8. Insérez du code pour récupérer le résultat de votre opération dans le RunWorkerCompleted gestionnaire d’événements.

    // This event handler demonstrates how to interpret
    // the outcome of the asynchronous operation implemented
    // in the DoWork event handler.
    private void backgroundWorker1_RunWorkerCompleted(
        object sender,
        RunWorkerCompletedEventArgs e)
    {
        if (e.Cancelled)
        {
            // The user canceled the operation.
            MessageBox.Show("Operation was canceled");
        }
        else if (e.Error != null)
        {
            // There was an error during the operation.
            string msg = String.Format("An error occurred: {0}", e.Error.Message);
            MessageBox.Show(msg);
        }
        else
        {
            // The operation completed normally.
            string msg = String.Format("Result = {0}", e.Result);
            MessageBox.Show(msg);
        }
    }
    
    ' This event handler demonstrates how to interpret 
    ' the outcome of the asynchronous operation implemented
    ' in the DoWork event handler.
    Private Sub backgroundWorker1_RunWorkerCompleted( _
    sender As Object, e As RunWorkerCompletedEventArgs) _
    Handles backgroundWorker1.RunWorkerCompleted
    
       If e.Cancelled Then
          ' The user canceled the operation.
          MessageBox.Show("Operation was canceled")
       ElseIf (e.Error IsNot Nothing) Then
          ' There was an error during the operation.
          Dim msg As String = String.Format("An error occurred: {0}", e.Error.Message)
          MessageBox.Show(msg)
       Else
          ' The operation completed normally.
          Dim msg As String = String.Format("Result = {0}", e.Result)
          MessageBox.Show(msg)
       End If
    End Sub   
    
  9. Implémentez la méthode TimeConsumingOperation.

    // This method models an operation that may take a long time
    // to run. It can be cancelled, it can raise an exception,
    // or it can exit normally and return a result. These outcomes
    // are chosen randomly.
    private int TimeConsumingOperation(
        BackgroundWorker bw,
        int sleepPeriod )
    {
        int result = 0;
    
        Random rand = new Random();
    
        while (!bw.CancellationPending)
        {
            bool exit = false;
    
            switch (rand.Next(3))
            {
                // Raise an exception.
                case 0:
                {
                    throw new Exception("An error condition occurred.");
                    break;
                }
    
                // Sleep for the number of milliseconds
                // specified by the sleepPeriod parameter.
                case 1:
                {
                    Thread.Sleep(sleepPeriod);
                    break;
                }
    
                // Exit and return normally.
                case 2:
                {
                    result = 23;
                    exit = true;
                    break;
                }
    
                default:
                {
                    break;
                }
            }
    
            if( exit )
            {
                break;
            }
        }
    
        return result;
    }
    
    ' This method models an operation that may take a long time 
    ' to run. It can be cancelled, it can raise an exception,
    ' or it can exit normally and return a result. These outcomes
    ' are chosen randomly.
    Private Function TimeConsumingOperation( _
    bw As BackgroundWorker, _
    sleepPeriod As Integer) As Integer
    
       Dim result As Integer = 0
       
       Dim rand As New Random()
       
         While Not bw.CancellationPending
             Dim [exit] As Boolean = False
    
             Select Case rand.Next(3)
                 ' Raise an exception.
                 Case 0
                     Throw New Exception("An error condition occurred.")
                     Exit While
    
                     ' Sleep for the number of milliseconds
                     ' specified by the sleepPeriod parameter.
                 Case 1
                     Thread.Sleep(sleepPeriod)
                     Exit While
    
                     ' Exit and return normally.
                 Case 2
                     result = 23
                     [exit] = True
                     Exit While
    
                 Case Else
                     Exit While
             End Select
    
             If [exit] Then
                 Exit While
             End If
         End While
       
       Return result
    End Function
    
  10. Dans le Concepteur Windows Forms, double-cliquez startButton pour créer le gestionnaire d’événements Click .

  11. Appelez la RunWorkerAsync méthode dans le gestionnaire d’événements Click pour startButton.

    private void startBtn_Click(object sender, EventArgs e)
    {
        this.backgroundWorker1.RunWorkerAsync(2000);
    }
    
    Private Sub startButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles startBtn.Click
        Me.backgroundWorker1.RunWorkerAsync(2000)
    End Sub
    
  12. Dans le Concepteur Windows Forms, double-cliquez cancelButton pour créer le gestionnaire d’événements Click .

  13. Appelez la CancelAsync méthode dans le gestionnaire d’événements Click pour cancelButton.

    private void cancelBtn_Click(object sender, EventArgs e)
    {
        this.backgroundWorker1.CancelAsync();
    }
    
    Private Sub cancelButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cancelBtn.Click
        Me.backgroundWorker1.CancelAsync()
    End Sub
    
  14. En haut du fichier, importez les espaces de noms System.ComponentModel et System.Threading.

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Threading;
    using System.Windows.Forms;
    
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Threading
    Imports System.Windows.Forms
    
  15. Appuyez sur F6 pour générer la solution, puis appuyez sur Ctrl+F5 pour exécuter l’application en dehors du débogueur.

    Remarque

    Si vous appuyez sur F5 pour exécuter l’application sous le débogueur, l’exception levée dans la TimeConsumingOperation méthode est interceptée et affichée par le débogueur. Lorsque vous exécutez l’application en dehors du débogueur, elle BackgroundWorker gère l’exception et la met en cache dans la Error propriété du RunWorkerCompletedEventArgs.

  16. Cliquez sur le bouton Démarrer pour exécuter une opération asynchrone, puis cliquez sur le bouton Annuler pour arrêter une opération asynchrone en cours d’exécution.

    Le résultat de chaque opération est affiché dans un MessageBox.

Étapes suivantes

Voir aussi