Procédure pas à pas : multithreading avec le composant BackgroundWorker (C# et Visual Basic)

Cette procédure pas à pas montre comment créer une application multithread qui recherche dans un fichier texte les occurrences d'un mot. Elle présente les points suivants :

  • Définition d'une classe à l'aide d'une méthode qui peut être appelée par le composant BackgroundWorker.

  • Gestion des événements déclenchés par le composant BackgroundWorker.

  • Démarrage d'un composant BackgroundWorker pour l'exécution d'une méthode.

  • Implémentation d'un bouton Cancel qui arrête le composant BackgroundWorker.

Pour créer l'interface utilisateur

  1. Ouvrez un nouveau projet d'application Windows Visual Basic ou C#, puis créez un formulaire nommé Form1.

  2. Ajoutez deux boutons et quatre zones de texte à Form1.

  3. Nommez les objets de la façon indiquée dans le tableau suivant.

    Objet

    Property

    Paramètre

    Premier bouton

    Name, Text

    Start, Start

    Deuxième bouton

    Name, Text

    Cancel, Cancel

    Première zone de texte

    Name, Text

    SourceFile, ""

    Deuxième zone de texte

    Name, Text

    CompareString, ""

    Troisième zone de texte

    Name, Text

    WordsCounted, "0"

    Quatrième zone de texte

    Name, Text

    LinesCounted, "0"

  4. Ajoutez une étiquette à côté de chaque zone de texte. Définissez la propriété Text pour chaque étiquette, comme illustré dans le tableau suivant.

    Objet

    Property

    Paramètre

    Première étiquette

    Text

    Source File

    Deuxième étiquette

    Text

    Compare String

    Troisième étiquette

    Text

    Matching Words

    Quatrième étiquette

    Text

    Lines Counted

Pour créer un composant BackgroundWorker et s'abonner à ses événements

  1. Ajoutez au formulaire un composant BackgroundWorker à partir de la section Composants de la Boîte à outils. Il apparaît dans la barre d'état des composants du formulaire.

  2. Définissez les propriétés suivantes de l'objet BackgroundWorker1 en Visual Basic ou de l'objet backgroundWorker1 en C#.

    Property

    Paramètre

    WorkerReportsProgress

    True

    WorkerSupportsCancellation

    True

  3. En C# uniquement, abonnez-vous aux événements de l'objet backgroundWorker1. En haut de la fenêtre Propriétés, cliquez sur l'icône Événements. Double-cliquez sur l'événement RunWorkerCompleted pour créer une méthode de gestionnaire d'événements. Effectuez la même opération pour les événements ProgressChanged et DoWork.

Pour définir la méthode qui s'exécutera sur un thread distinct

  1. Dans le menu Projet, sélectionnez Ajouter une classe pour ajouter une classe au projet. La boîte de dialogue Ajouter un nouvel élément s'affiche.

  2. Sélectionnez Classe dans la fenêtre de modèles, puis tapez Words.vb ou Words.cs dans le champ Nom.

  3. Cliquez sur Ajouter. La classe Words s'affiche.

  4. Ajoutez le code suivant à la classe Words :

    Public Class Words
        ' Object to store the current state, for passing to the caller. 
        Public Class CurrentState
            Public LinesCounted As Integer 
            Public WordsMatched As Integer 
        End Class 
    
        Public SourceFile As String 
        Public CompareString As String 
        Private WordCount As Integer = 0
        Private LinesCounted As Integer = 0
    
        Public Sub CountWords(
            ByVal worker As System.ComponentModel.BackgroundWorker,
            ByVal e As System.ComponentModel.DoWorkEventArgs
        )
            ' Initialize the variables. 
            Dim state As New CurrentState
            Dim line = "" 
            Dim elapsedTime = 20
            Dim lastReportDateTime = Now
    
            If CompareString Is Nothing OrElse
               CompareString = System.String.Empty Then 
    
               Throw New Exception("CompareString not specified.")
            End If 
    
            Using myStream As New System.IO.StreamReader(SourceFile)
    
                ' Process lines while there are lines remaining in the file. 
                Do While Not myStream.EndOfStream
                    If worker.CancellationPending Then
                        e.Cancel = True 
                        Exit Do 
                    Else
                        line = myStream.ReadLine
                        WordCount += CountInString(line, CompareString)
                        LinesCounted += 1
    
                        ' Raise an event so the form can monitor progress. 
                        If Now > lastReportDateTime.AddMilliseconds(elapsedTime) Then
                            state.LinesCounted = LinesCounted
                            state.WordsMatched = WordCount
                            worker.ReportProgress(0, state)
                            lastReportDateTime = Now
                        End If 
    
                        ' Uncomment for testing. 
                        'System.Threading.Thread.Sleep(5) 
                    End If 
                Loop 
    
                ' Report the final count values.
                state.LinesCounted = LinesCounted
                state.WordsMatched = WordCount
                worker.ReportProgress(0, state)
            End Using 
        End Sub 
    
        Private Function CountInString(
            ByVal SourceString As String,
            ByVal CompareString As String
        ) As Integer 
            ' This function counts the number of times 
            ' a word is found in a line. 
            If SourceString Is Nothing Then 
                Return 0
            End If 
    
            Dim EscapedCompareString =
                System.Text.RegularExpressions.Regex.Escape(CompareString)
    
            ' To count all occurrences of the string, even within words, remove 
            ' both instances of "\b". 
            Dim regex As New System.Text.RegularExpressions.Regex(
                "\b" + EscapedCompareString + "\b",
                System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    
            Dim matches As System.Text.RegularExpressions.MatchCollection
            matches = regex.Matches(SourceString)
            Return matches.Count
        End Function 
    End Class
    
    public class Words
    {
        // Object to store the current state, for passing to the caller. 
        public class CurrentState
        {
            public int LinesCounted;
            public int WordsMatched;
        }
    
        public string SourceFile;
        public string CompareString;
        private int WordCount;
        private int LinesCounted;
    
        public void CountWords(
            System.ComponentModel.BackgroundWorker worker,
            System.ComponentModel.DoWorkEventArgs e)
        {
            // Initialize the variables.
            CurrentState state = new CurrentState();
            string line = "";
            int elapsedTime = 20;
            DateTime lastReportDateTime = DateTime.Now;
    
            if (CompareString == null ||
                CompareString == System.String.Empty)
            {
                throw new Exception("CompareString not specified.");
            }
    
            // Open a new stream. 
            using (System.IO.StreamReader myStream = new System.IO.StreamReader(SourceFile))
            {
                // Process lines while there are lines remaining in the file. 
                while (!myStream.EndOfStream)
                {
                    if (worker.CancellationPending)
                    {
                        e.Cancel = true;
                        break;
                    }
                    else
                    {
                        line = myStream.ReadLine();
                        WordCount += CountInString(line, CompareString);
                        LinesCounted += 1;
    
                        // Raise an event so the form can monitor progress. 
                        int compare = DateTime.Compare(
                            DateTime.Now, lastReportDateTime.AddMilliseconds(elapsedTime));
                        if (compare > 0)
                        {
                            state.LinesCounted = LinesCounted;
                            state.WordsMatched = WordCount;
                            worker.ReportProgress(0, state);
                            lastReportDateTime = DateTime.Now;
                        }
                    }
                    // Uncomment for testing. 
                    //System.Threading.Thread.Sleep(5);
                }
    
                // Report the final count values.
                state.LinesCounted = LinesCounted;
                state.WordsMatched = WordCount;
                worker.ReportProgress(0, state);
            }
        }
    
    
        private int CountInString(
            string SourceString,
            string CompareString)
        {
            // This function counts the number of times 
            // a word is found in a line. 
            if (SourceString == null)
            {
                return 0;
            }
    
            string EscapedCompareString =
                System.Text.RegularExpressions.Regex.Escape(CompareString);
    
            System.Text.RegularExpressions.Regex regex;
            regex = new System.Text.RegularExpressions.Regex( 
                // To count all occurrences of the string, even within words, remove 
                // both instances of @"\b" from the following line.
                @"\b" + EscapedCompareString + @"\b",
                System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    
            System.Text.RegularExpressions.MatchCollection matches;
            matches = regex.Matches(SourceString);
            return matches.Count;
        }
    
    }
    

Pour gérer les événements à partir du thread

  • Ajoutez les gestionnaires d'événements suivants au formulaire principal :

    Private Sub BackgroundWorker1_RunWorkerCompleted( 
        ByVal sender As Object, 
        ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs
      ) Handles BackgroundWorker1.RunWorkerCompleted
    
        ' This event handler is called when the background thread finishes. 
        ' This method runs on the main thread. 
        If e.Error IsNot Nothing Then
            MessageBox.Show("Error: " & e.Error.Message)
        ElseIf e.Cancelled Then
            MessageBox.Show("Word counting canceled.")
        Else
            MessageBox.Show("Finished counting words.")
        End If 
    End Sub 
    
    Private Sub BackgroundWorker1_ProgressChanged( 
        ByVal sender As Object, 
        ByVal e As System.ComponentModel.ProgressChangedEventArgs
      ) Handles BackgroundWorker1.ProgressChanged
    
        ' This method runs on the main thread. 
        Dim state As Words.CurrentState = 
            CType(e.UserState, Words.CurrentState)
        Me.LinesCounted.Text = state.LinesCounted.ToString
        Me.WordsCounted.Text = state.WordsMatched.ToString
    End Sub
    
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    // This event handler is called when the background thread finishes. 
    // This method runs on the main thread. 
    if (e.Error != null)
        MessageBox.Show("Error: " + e.Error.Message);
    else if (e.Cancelled)
        MessageBox.Show("Word counting canceled.");
    else
        MessageBox.Show("Finished counting words.");
    }
    
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // This method runs on the main thread.
        Words.CurrentState state =
            (Words.CurrentState)e.UserState;
        this.LinesCounted.Text = state.LinesCounted.ToString();
        this.WordsCounted.Text = state.WordsMatched.ToString();
    }
    

Pour démarrer et appeler un nouveau thread qui exécute la méthode WordCount

  1. Ajoutez les procédures suivantes à votre programme :

    Private Sub BackgroundWorker1_DoWork( 
        ByVal sender As Object, 
        ByVal e As System.ComponentModel.DoWorkEventArgs
      ) Handles BackgroundWorker1.DoWork
    
        ' This event handler is where the actual work is done. 
        ' This method runs on the background thread. 
    
        ' Get the BackgroundWorker object that raised this event. 
        Dim worker As System.ComponentModel.BackgroundWorker
        worker = CType(sender, System.ComponentModel.BackgroundWorker)
    
        ' Get the Words object and call the main method. 
        Dim WC As Words = CType(e.Argument, Words)
        WC.CountWords(worker, e)
    End Sub 
    
    Sub StartThread()
        ' This method runs on the main thread. 
        Me.WordsCounted.Text = "0" 
    
        ' Initialize the object that the background worker calls. 
        Dim WC As New Words
        WC.CompareString = Me.CompareString.Text
        WC.SourceFile = Me.SourceFile.Text
    
        ' Start the asynchronous operation.
        BackgroundWorker1.RunWorkerAsync(WC)
    End Sub
    
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // This event handler is where the actual work is done. 
        // This method runs on the background thread. 
    
        // Get the BackgroundWorker object that raised this event.
        System.ComponentModel.BackgroundWorker worker;
        worker = (System.ComponentModel.BackgroundWorker)sender;
    
        // Get the Words object and call the main method.
        Words WC = (Words)e.Argument;
        WC.CountWords(worker, e);
    }
    
    private void StartThread()
    {
        // This method runs on the main thread. 
        this.WordsCounted.Text = "0";
    
        // Initialize the object that the background worker calls.
        Words WC = new Words();
        WC.CompareString = this.CompareString.Text;
        WC.SourceFile = this.SourceFile.Text;
    
        // Start the asynchronous operation.
        backgroundWorker1.RunWorkerAsync(WC);
    }
    
  2. Appelez la méthode StartThread à partir du bouton Start sur le formulaire :

    Private Sub Start_Click() Handles Start.Click
        StartThread()
    End Sub
    
    private void Start_Click(object sender, EventArgs e)
    {
        StartThread();
    }
    

Pour implémenter un bouton Cancel qui arrête le thread

  • Appelez la procédure StopThread à partir du gestionnaire d'événements Click pour le bouton Cancel.

    Private Sub Cancel_Click() Handles Cancel.Click
        ' Cancel the asynchronous operation. 
        Me.BackgroundWorker1.CancelAsync()
    End Sub
    
    private void Cancel_Click(object sender, EventArgs e)
    {
        // Cancel the asynchronous operation. 
        this.backgroundWorker1.CancelAsync();
    }
    

Test

Vous pouvez à présent tester l'application afin de vous assurer qu'elle fonctionne correctement.

Pour tester l'application

  1. Appuyez sur F5 pour exécuter l'application.

  2. Quand le formulaire s'affiche, entrez le chemin d'accès à un fichier de votre choix pour réaliser le test dans la zone sourceFile. Par exemple, supposons que le fichier de test se nomme Test.txt, entrez C:\Test.txt.

  3. Dans la deuxième zone de texte, entrez un mot ou une expression que l'application va devoir rechercher dans le fichier texte.

  4. Cliquez sur le bouton Start. Sur le bouton LinesCounted, la valeur affichée devrait commencer immédiatement à être incrémentée. L'application affiche le message « Finished Counting » une fois terminé.

Pour tester le bouton Cancel

  1. Appuyez sur F5 pour démarrer l'application, puis entrez le nom de fichier et le mot à rechercher comme le décrit la procédure précédente. Assurez-vous que le fichier choisi est suffisamment volumineux pour que vous ayez le temps d'annuler la procédure avant qu'elle ne soit terminée.

  2. Cliquez sur le bouton Start pour lancer l'application.

  3. Cliquez sur le bouton Cancel. L'application devrait cesser le décompte immédiatement.

Étapes suivantes

Cette application contient une fonctionnalité de base de gestion des erreurs. Elle détecte les chaînes de recherche vides. Vous pouvez ajouter robustesse à ce programme en lui permettant de gérer d'autres erreurs, telles que le dépassement du nombre maximal de mots ou de lignes pouvant être dénombrés.

Voir aussi

Tâches

Procédure pas à pas : création d'un composant simple multithread avec Visual Basic

Comment : s'abonner et annuler l'abonnement à des événements (Guide de programmation C#)

Autres ressources

Threads (C# et Visual Basic)