共用方式為


逐步解說:使用 BackgroundWorker 元件進行多執行緒處理 (C# 和 Visual Basic)

本逐步解說示範如何建立多執行緒應用程式 (Multithreaded Application),以搜尋文字檔中的相符文字。 其中將示範:

若要建立使用者介面

  1. 開啟新的 Visual Basic 或 C# Windows 應用程式專案,並建立名為 Form1 的表單。

  2. 將兩個按鈕和四個文字方塊加入 Form1。

  3. 如下表所示,為物件命名。

    物件

    屬性

    設定

    第一個按鈕

    Name, Text

    Start、Start

    第二個按鈕

    Name, Text

    Cancel、Cancel

    第一個文字方塊

    Name, Text

    SourceFile、""

    第二個文字方塊

    Name, Text

    CompareString、""

    第三個文字方塊

    Name, Text

    WordsCounted、"0"

    第四個文字方塊

    Name, Text

    LinesCounted、"0"

  4. 在每一個文字方塊旁各加上一個標籤。 設定每一個標籤的 Text 屬性,如下表所示。

    物件

    屬性

    設定

    第一個標籤

    Text

    Source File

    第二個標籤

    Text

    Compare String

    第三個標籤

    Text

    Matching Words

    第四個標籤

    Text

    Lines Counted

若要建立 BackgroundWorker 元件並訂閱其事件

  1. 從 [工具箱] 的 [元件] 區段將 BackgroundWorker 元件加入至表單中。 這個元件將會出現在表單的元件匣裡。

  2. 在 Visual Basic 中設定 BackgroundWorker1 物件的下列屬性,或在 C# 中設定 backgroundWorker1 物件的下列屬性。

    屬性

    設定

    WorkerReportsProgress

    True

    WorkerSupportsCancellation

    True

  3. (僅限 C#) 訂閱 backgroundWorker1 物件的事件。 在 [屬性] 視窗的頂端,按一下 [事件] 圖示。 按兩下 RunWorkerCompleted 事件以建立事件處理常式方法。 對 ProgressChanged 和 DoWork 事件執行相同步驟。

若要定義在個別執行緒上執行的方法

  1. 從 [專案] 功能表,選擇 [加入類別] 來將類別加入專案中。 接著會顯示 [加入新項目] 對話方塊。

  2. 在範本視窗中選取 [類別],並在名稱欄位中輸入 Words.vb 或 Words.cs。

  3. 按一下 [加入]。 即顯示 Words 類別。

  4. 將下列程式碼加入至 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)
    
            Dim regex As New System.Text.RegularExpressions.Regex(
                EscapedCompareString,
                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( 
                EscapedCompareString,
                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 方法的新執行緒

  1. 將下列程序加入至您的程式中:

    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. 從您表單上的 [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();
    }
    

測試

現在您可以測試應用程式,以確定程式運作正常。

若要測試應用程式

  1. 請按 F5 執行應用程式。

  2. 顯示表單時,請在 [sourceFile] 方塊中,輸入您想要測試檔案的檔案路徑。 例如,假設您測試檔的名稱為 Test.txt,請輸入 C:\Test.txt。

  3. 在第二個文字方塊中,輸入要在文字檔中搜尋的應用程式的文字或片語。

  4. 按一下 [Start] 按鈕。 [LinesCounted] 按鈕應該會立刻開始遞增。 動作結束時,應用程式會出現「完成計數」訊息。

若要測試 Cancel 按鈕

  1. 按 F5 啟動應用程式,並依照之前的程序,輸入檔案名稱和搜尋文字。 請確定您選擇的檔案夠大,以確保檔案結束之前,會有時間能夠取消這個程序。

  2. 按一下 [Start] 按鈕,啟動應用程式。

  3. 按一下 [Cancel] 按鈕。 應用程式應該會立刻停止計數。

後續步驟

本應用程式包含某些基本的錯誤處理。 它會偵測空白的搜尋字串。 藉由處理其他錯誤,例如超過可計數的最大字數或行數,可讓此程式更為強固。

請參閱

工作

逐步解說:使用 Visual Basic 撰寫簡單的多執行緒元件

HOW TO:訂閱及取消訂閱事件 (C# 程式設計手冊)

參考

執行緒 (C# 和 Visual Basic)