演练:利用 BackgroundWorker 组件进行多线程处理(C# 和 Visual Basic)
该演练说明如何创建一个多线程应用程序,该程序在文本文件中搜索字词的匹配项。它演示:
用可以由 BackgroundWorker 组件调用的方法定义一个类。
处理由 BackgroundWorker 组件引发的事件。
启动 BackgroundWorker 组件以运行方法。
实现停止 BackgroundWorker 组件的 Cancel 按钮。
创建用户界面
打开新的 Visual Basic 或 C# Windows 应用程序项目,并创建一个名为 Form1 的窗体。
向 Form1 中添加两个按钮和四个文本框。
如下表所示命名对象。
对象
属性
设置
第一个按钮
Name, Text
Start, Start
第二个按钮
Name, Text
Cancel, Cancel
第一个文本框
Name, Text
SourceFile, ""
第二个文本框
Name, Text
CompareString, ""
第三个文本框
Name, Text
WordsCounted, "0"
第四个文本框
Name, Text
LinesCounted, "0"
近邻每个文本框添加一个标签。为每个标签设置 Text 属性,如下表所示。
对象
属性
设置
第一个标签
Text
源文件
第二个标签
Text
Compare String
第三个标签
Text
Matching Words
第四个标签
Text
Lines Counted
创建 BackgroundWorker 组件并订阅其事件
将**“工具箱”的“组件”**部分中的 BackgroundWorker 组件添加到窗体。它将出现在窗体的组件栏中。
为 Visual Basic 中的 BackgroundWorker1 对象或 C# 中的 backgroundWorker1 对象设置以下属性。
属性
设置
WorkerReportsProgress
True
WorkerSupportsCancellation
True
(仅在 C#中)订阅 backgroundWorker1 对象的事件。在**“属性”窗口的顶部,单击“事件”**图标。双击 RunWorkerCompleted 事件以创建事件处理程序方法。为 ProgressChanged 和 DoWork 事件执行相同操作。
定义将在单独的线程上运行的方法
从**“项目”菜单中选择“添加类”,在项目中添加类。显示“添加新项”**对话框。
从模板窗口选择**“类”**,并在名称字段内键入 Words.vb 或 Words.cs。
单击**“添加”**。显示 Words 类。
将下面的代码添加到 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; } }
处理线程中的事件
将下面的事件处理程序添加到主窗体中:
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(); }
启动并调用运行 WordCount 方法的新线程
将下面的过程添加到程序中:
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); }
通过窗体上的 Start 按钮调用 StartThread 方法:
Private Sub Start_Click() Handles Start.Click StartThread() End Sub
private void Start_Click(object sender, EventArgs e) { StartThread(); }
实现停止线程的 Cancel 按钮
从 Cancel 按钮的 Click 事件处理程序中调用 StopThread 过程。
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(); }
测试
现在可以测试该应用程序以确保其正确运行。
测试应用程序
按 F5 运行该应用程序。
显示窗体时,在 sourceFile 框中输入要测试的文件的路径。例如,假定测试文件名为 Test.txt,则输入 C:\Test.txt。
在第二个文本框中,输入要让该应用程序在文本文件中搜索的字词或短语。
单击 Start 按钮。LinesCounted 按钮应当立即开始递增。在任务完成后,应用程序将显示“计数完成”(Finished Counting) 消息。
测试 Cancel 按钮
按 F5 以启动该应用程序,按照前面过程中所述内容输入文件名和搜索字词。确保所选择的文件足够大,以确保在搜索结束之前有时间取消该过程。
单击Start按钮启动该应用程序。
单击 Cancel 按钮。应用程序应该立即停止计数。
后续步骤
此应用程序包含一些基本错误处理。它检测空白搜索字符串。可以通过处理其他错误(如超过可以计数的最大字词数或行数)使该程序更加可靠。