Exemplarische Vorgehensweise: Multithreading mit der BackgroundWorker-Komponente (C# und Visual Basic)

In dieser exemplarischen Vorgehensweise wird die Erstellung einer Multithreadanwendung beschrieben, die eine Textdatei nach Vorkommen eines Wortes durchsucht.Folgende Vorgänge werden veranschaulicht:

  • Definieren einer Klasse mit einer Methode, die von der BackgroundWorker-Komponente aufgerufen werden kann.

  • Das Behandeln von Ereignissen, die von der BackgroundWorker-Komponente ausgelöst werden.

  • Das Starten einer BackgroundWorker-Komponente zum Ausführen einer Methode.

  • Das Implementieren einer Cancel-Schaltfläche, mit der die Ausführung der BackgroundWorker-Komponente angehalten wird.

So erstellen Sie eine Benutzeroberfläche

  1. Öffnen Sie in Visual Basic oder in Visual C# ein neues Windows-Anwendungsprojekt, und erstellen Sie ein Formular mit dem Namen Form1.

  2. Fügen Sie Form1 zwei Schaltflächen und vier Textfelder hinzu.

  3. Benennen Sie die Objekte wie in der folgenden Tabelle:

    Objekt

    Property

    Einstellung

    Erste Schaltfläche

    Name, Text

    Start, Start

    Zweite Schaltfläche

    Name, Text

    Cancel, Cancel

    Erstes Textfeld

    Name, Text

    SourceFile, ""

    Zweites Textfeld

    Name, Text

    CompareString, ""

    Drittes Textfeld

    Name, Text

    WordsCounted, "0"

    Viertes Textfeld

    Name, Text

    LinesCounted, "0"

  4. Fügen Sie neben jedem Textfeld eine Bezeichnung hinzu.Legen Sie die Text-Eigenschaft für jede Bezeichnung wie in der folgenden Tabelle fest.

    Objekt

    Property

    Einstellung

    Erste Bezeichnung

    Text

    Quelldatei

    Zweite Bezeichnung

    Text

    Zeichenfolge vergleichen

    Dritte Bezeichnung

    Text

    Matching Words

    Vierte Bezeichnung

    Text

    Lines Counted

So erstellen Sie eine BackgroundWorker-Komponente und abonnieren die zugehörigen Ereignisse

  1. Fügen Sie dem Formular aus dem Abschnitt Komponenten der Toolbox eine BackgroundWorker-Komponente hinzu.Sie wird auf der Komponentenleiste des Formulars angezeigt.

  2. Legen Sie die folgenden Eigenschaften für das BackgroundWorker1-Objekt in Visual Basic bzw. für das backgroundWorker1-Objekt in C# fest.

    Property

    Einstellung

    WorkerReportsProgress

    True

    WorkerSupportsCancellation

    True

  3. Nur C#: Abonnieren Sie die Ereignisse des backgroundWorker1-Objekts.Klicken Sie oben im Fenster Eigenschaften auf das Symbol Ereignisse.Doppelklicken Sie auf das RunWorkerCompleted-Ereignis, um eine Ereignishandlermethode zu erstellen.Wiederholen Sie das Verfahren für das ProgressChanged-Ereignis und das DoWork-Ereignis.

So definieren Sie die Methode, die in einem separaten Thread ausgeführt wird

  1. Klicken Sie im Menü Projekt auf Klasse hinzufügen, um dem Projekt eine Klasse hinzuzufügen.Das Dialogfeld Neues Element hinzufügen wird angezeigt.

  2. Wählen Sie im Vorlagenfenster Klasse aus, und geben Sie in das Namensfeld Words.vb bzw. Words.cs ein.

  3. Klicken Sie auf Hinzufügen.Die Words-Klasse wird angezeigt.

  4. Fügen Sie der Words-Klasse folgenden Code hinzu:

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

So behandeln Sie Ereignisse aus dem Thread

  • Fügen Sie dem Hauptformular die folgenden Ereignishandler hinzu:

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

So starten und rufen Sie einen neuen Thread auf, der die WordCount-Methode ausführt

  1. Fügen Sie dem Programm die folgenden Prozeduren hinzu:

    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. Rufen Sie über die Start-Schaltfläche im Formular die StartThread-Methode auf:

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

So implementieren Sie eine Cancel-Schaltfläche, die den Thread beendet

  • Rufen Sie mit dem Click-Ereignishandler die StopThread-Prozedur für die Cancel-Schaltfläche auf:

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

Testen

Jetzt können Sie die Anwendung testen, um sicherzustellen, dass sie ordnungsgemäß funktioniert.

So testen Sie die Anwendung

  1. Drücken Sie F5, um die Anwendung auszuführen.

  2. Wenn das Formular angezeigt wird, geben Sie im sourceFile-Feld den Dateipfad für die zu testende Datei ein.Für eine Testdatei mit der Bezeichnung Test.txt geben Sie z. B. C:\Test.txt ein.

  3. In das zweite Textfeld geben Sie ein Wort oder eine Wortgruppe ein, nach der die Anwendung die Textdatei durchsuchen soll.

  4. Klicken Sie auf die Schaltfläche Start.Der Wert der Schaltfläche LinesCounted sollte unverzüglich schrittweise erhöht werden.Nachdem der Vorgang beendet ist, wird in der Anwendung eine Meldung angezeigt, die darauf hinweist, dass die Zählung abgeschlossen ist.

So testen Sie die Schaltfläche "Cancel"

  1. Drücken Sie F5, um die Anwendung zu starten, und geben Sie Dateiname und Suchbegriff ein (siehe oben stehende Schrittfolge).Vergewissern Sie sich, dass die verwendete Datei groß genug ist, damit Sie genügend Zeit haben, den Suchvorgang abzubrechen, bevor er beendet ist.

  2. Klicken Sie auf die Schaltfläche Start, um die Anwendung zu starten.

  3. Klicken Sie auf die Schaltfläche Cancel.Die Anwendung sollte den Zählvorgang sofort abbrechen.

Nächste Schritte

Diese Anwendung umfasst einige grundlegende Fehlerbehandlungsmethoden.Sie erkennt leere Suchzeichenfolgen.Damit die Anwendung insgesamt stabiler läuft, können auch andere Fehler (z. B. das Überschreiten der maximalen Anzahl zählbarer Wörter oder Zeilen) behandelt werden.

Siehe auch

Aufgaben

Exemplarische Vorgehensweise: Erstellen einer einfachen Multithreadkomponente mit Visual Basic

Gewusst wie: Abonnieren von Ereignissen und Kündigen von Ereignisabonnements (C#-Programmierhandbuch)

Weitere Ressourcen

Threading (C# und Visual Basic)