Updated: July 2008
This walkthrough demonstrates how to create a multithreaded application that searches a text file for occurrences of a word. It demonstrates:
To build the code example for this topic
Open a new Visual Basic Windows Application project, and create a form named Form1.
Add two buttons and four text boxes to Form1.
Name the objects as shown in the following table.
Object
|
Property
|
Setting
|
|---|
First button
|
Name, Text
|
Start, Start
|
Second button
|
Name, Text
|
Cancel, Cancel
|
First text box
|
Name, Text
|
SourceFile, ""
|
Second text box
|
Name, Text
|
CompareString, ""
|
Third text box
|
Name, Text
|
WordsCounted, "0"
|
Fourth text box
|
Name, Text
|
LinesCounted, "0"
|
Add a label next to each text box. Set the Text property for each label as shown in the following table.
Object
|
Property
|
Setting
|
|---|
First label
|
Text
|
Source File
|
Second label
|
Text
|
Compare String
|
Third label
|
Text
|
Matching Words
|
Fourth label
|
Text
|
Lines Counted
|
Add a BackgroundWorker component from the Components section of the ToolBox to your form. It will appear in the form's component tray.
Set the following properties for the BackgroundWorker1 object.
Property
|
Setting
|
|---|
WorkerReportsProgress
|
True
|
WorkerSupportsCancellation
|
True
|
To define the method that will run on a separate thread
From the Project menu, choose Add Class to add a class to the project. The Add New Item dialog box is displayed.
Select Class from the templates window and type Words.vb in the name field.
Click Add. The Words class is displayed.
Add an Option Compare statement to the top of the Words class, above the Class statement:
Option Compare Text ' Case insensitive search.
' Use Option Compare Binary for case sensitive search.
Add the following code to the Words class:
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 myStream As System.IO.StreamReader = Nothing
Dim line = ""
Dim elapsedTime = 20
Dim lastReportDateTime = Now
If CompareString Is Nothing Or _
CompareString = System.String.Empty Then
Throw New Exception("CompareString not specified.")
End If
Try
' Open a new stream.
myStream = My.Computer.FileSystem.OpenTextFileReader(SourceFile)
' Do while there are lines remaining in the file to be read.
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.AddMilliseconds(elapsedTime)
End If
End If
Loop
' Report the final count values.
state.LinesCounted = LinesCounted
state.WordsMatched = WordCount
worker.ReportProgress(0, state)
Finally
If myStream IsNot Nothing Then
' Close the file.
myStream.Close()
End If
End Try
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 regex As New System.Text.RegularExpressions.Regex( _
System.Text.RegularExpressions.Regex.Escape(CompareString))
Dim matches As System.Text.RegularExpressions.MatchCollection
matches = regex.Matches(SourceString)
Return matches.Count
End Function
End Class
To handle events from the thread
To start and call a new thread that runs the WordCount method
Add the following procedures to your program:
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 Works 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
Call the StartThread method from the Start button on your form:
Private Sub Start_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Start.Click
StartThread()
End Sub
To implement a Cancel button that stops the thread
You can now test the application to make sure it works correctly.
To test the application
Press F5 to run the application.
When the form is displayed, enter the file path for the file you want to test in the sourceFile box. For example, assuming your test file is named Test.txt, enter C:\Test.txt.
In the second text box, enter a word or phrase for the application to search for in the text file.
Click the Start button. The LinesCounted button should begin incrementing immediately. The application displays the message "Finished Counting" when it is done.
To test the Cancel button
Press F5 to start the application, and enter the file name and search word as described in the previous procedure. Make sure that the file you choose is large enough to ensure you will have time to cancel the procedure before it is finished.
Click the Start button to start the application.
Click the Cancel button. The application should stop counting immediately.
This application contains some basic error handling. It detects blank search strings. You can make this program more robust by handling other errors, such as exceeding the maximum number of words or lines that can be counted.
Tasks
Other Resources
Date
|
History
|
Reason
|
|---|
July 2008
|
Fixed errors in the WordCount method of the example.
|
Customer feedback.
|