Share via


Procedura dettagliata: multithreading con il componente BackgroundWorker (C# e Visual Basic)

In questa procedura dettagliata viene illustrata la creazione di un'applicazione con multithreading che cerca occorrenze di una parola in un file di testo. Vengono illustrate le funzionalità riportate di seguito.

  • Definizione di una classe con un metodo che può essere chiamato dal componente BackgroundWorker.

  • Gestione di eventi generati dal componente BackgroundWorker.

  • Avvio di un componente BackgroundWorker per l'esecuzione di un metodo.

  • Implementazione di un pulsante Cancel che consente di arrestare il componente BackgroundWorker.

Per creare l'interfaccia utente

  1. Aprire un nuovo progetto Applicazione Windows in Visual Basic o C# e creare un form denominato Form1.

  2. Aggiungere due pulsanti e quattro caselle di testo a Form1.

  3. Assegnare un nome agli oggetti, come illustrato nella tabella riportata di seguito.

    Object

    Proprietà

    Impostazione

    Primo pulsante

    Name, Text

    Start, Start

    Secondo pulsante

    Name, Text

    Cancel, Cancel

    Prima casella di testo

    Name, Text

    SourceFile, ""

    Seconda casella di testo

    Name, Text

    CompareString, ""

    Terza casella di testo

    Name, Text

    WordsCounted, "0"

    Quarta casella di testo

    Name, Text

    LinesCounted, "0"

  4. Aggiungere un'etichetta accanto a ogni casella di testo. Impostare la proprietà Text per ogni casella, come illustrato nella tabella riportata di seguito.

    Object

    Proprietà

    Impostazione

    Prima etichetta

    Text

    File di origine

    Seconda etichetta

    Text

    Compare String

    Terza etichetta

    Text

    Matching Words

    Quarta etichetta

    Text

    Lines Counted

Per creare un componente BackgroundWorker e sottoscriverne gli eventi

  1. Aggiungere al form un componente BackgroundWorker dalla sezione Componenti della Casella degli strumenti. Il componente verrà visualizzato nella barra componenti del form.

  2. Impostare le proprietà seguenti per l'oggetto BackgroundWorker1 in Visual Basic o backgroundWorker1 in C#.

    Proprietà

    Impostazione

    WorkerReportsProgress

    True

    WorkerSupportsCancellation

    True

  3. Solo in C#, sottoscrivere gli eventi dell'oggetto backgroundWorker1. Nella parte superiore della finestra Proprietà fare clic sull'icona Eventi. Fare doppio clic sull'evento RunWorkerCompleted per creare un metodo del gestore eventi. Eseguire la stessa operazione per gli eventi ProgressChanged e DoWork.

Per definire il metodo che verrà eseguito in un thread distinto

  1. Scegliere Aggiungi classe dal menu Progetto per aggiungere una classe al progetto. Verrà visualizzata la finestra di dialogo Aggiungi nuovo elemento.

  2. Selezionare Classe nella finestra dei modelli e digitare Words.vb o Words.cs nel campo del nome.

  3. Scegliere Aggiungi. Verrà visualizzata la classe Words.

  4. Aggiungere il codice seguente alla 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;
        }
    
    }
    

Per gestire eventi dal thread

  • Aggiungere i gestori eventi seguenti al form principale:

    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();
    }
    

Per avviare e chiamare un nuovo thread ed eseguire il metodo WordCount

  1. Aggiungere le seguenti routine al programma:

    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. Chiamare il metodo StartThread dal pulsante Start nel form:

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

Per implementare un pulsante Cancel e arrestare il thread

  • Chiamare la routine StopThread dal gestore dell'evento Click per il pulsante 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

È ora possibile verificare l'applicazione per assicurarsi che funzioni correttamente.

Per eseguire il test dell'applicazione

  1. ‎Premere F5 per eseguire l'applicazione.

  2. Quando verrà visualizzato il form, immettere il percorso del file per cui si desidera eseguire la verifica nella casella sourceFile. Se ad esempio il file da verificare è denominato Test.txt, immettere C:\Test.txt.

  3. Nella seconda casella di testo immettere una parola o una frase da cercare nel file di testo.

  4. Fare clic sul pulsante Start. L'incremento del pulsante LinesCounted dovrebbe iniziare immediatamente. Al termine dell'operazione verrà visualizzato un messaggio per informare che il conteggio è terminato.

Per eseguire il test del pulsante Cancel

  1. Premere F5 per avviare l'applicazione e immettere il nome del file e la parola da cercare, come descritto nella routine precedente. Assicurarsi che il file selezionato abbia una dimensione sufficiente per consentire di annullare la routine prima del completamento.

  2. Fare clic sul pulsante Start per avviare l'applicazione.

  3. Fare clic sul pulsante Cancel. Il conteggio dovrebbe arrestarsi immediatamente.

Passaggi successivi

Nell'applicazione sono disponibili alcune funzionalità di base per la gestione degli errori, Individua stringhe di ricerca vuote. È possibile rendere più affidabile il programma tramite la gestione di altri errori, quale il superamento del numero massimo di parole o righe di cui è consentito il conteggio.

Vedere anche

Attività

Procedura dettagliata: modifica di componenti multithreading semplici con Visual Basic

Procedura: sottoscrivere e annullare la sottoscrizione di eventi (Guida per programmatori C#)

Altre risorse

Threading (C# e Visual Basic)