연습: BackgroundWorker 구성 요소를 사용한 다중 스레딩(C# 및 Visual Basic)

이 연습에서는 텍스트 파일에서 특정 단어를 검색하는 다중 스레드 응용 프로그램을 만드는 방법을 설명합니다. 내용은 다음과 같습니다.

사용자 인터페이스를 만들려면

  1. Visual Basic 또는 C# Windows 응용 프로그램 프로젝트를 새로 열고 Form1이라는 폼을 만듭니다.

  2. Form1에 두 개의 단추와 네 개의 텍스트 상자를 추가합니다.

  3. 다음 표에 표시된 대로 개체를 명명합니다.

    Object

    Property

    설정

    첫 번째 단추

    Name, Text

    Start, Start

    두 번째 단추

    Name, Text

    Cancel, Cancel

    첫 번째 텍스트 상자

    Name, Text

    SourceFile, ""

    두 번째 텍스트 상자

    Name, Text

    CompareString, ""

    세 번째 텍스트 상자

    Name, Text

    WordsCounted, "0"

    네 번째 텍스트 상자

    Name, Text

    LinesCounted, "0"

  4. 각 텍스트 상자 옆에 레이블을 추가합니다. 다음 표와 같이 각 레이블에 Text 속성을 설정합니다.

    Object

    Property

    설정

    첫 번째 레이블

    Text

    소스 파일

    두 번째 레이블

    Text

    Compare String

    세 번째 레이블

    Text

    Matching Words

    네 번째 레이블

    Text

    Lines Counted

BackgroundWorker 구성 요소를 만들고 해당 이벤트를 구독하려면

  1. 도구 상자구성 요소 섹션에 있는 BackgroundWorker 구성 요소를 폼에 추가합니다. 이 구성 요소가 폼의 구성 요소 트레이에 나타납니다.

  2. Visual Basic의 BackgroundWorker1 개체나 C#의 backgroundWorker1 개체에 대해 다음 속성을 설정합니다.

    Property

    설정

    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 단추의 숫자가 즉시 증가됩니다. 이 작업이 끝나면 응용 프로그램은 "Finished Counting" 메시지를 표시합니다.

Cancel 단추를 테스트하려면

  1. F5 키를 눌러 응용 프로그램을 시작하고 이전 프로시저에서 설명한 대로 파일 이름과 검색 단어를 입력합니다. 이 작업이 끝나기 전에 프로시저를 취소할 수 있는 시간이 있는지를 확인하기 위해 선택한 파일의 크기가 충분한지 확인합니다.

  2. Start 단추를 클릭하여 응용 프로그램을 시작합니다.

  3. Cancel 단추를 클릭합니다. 응용 프로그램이 즉시 카운트를 중지합니다.

다음 단계

이 응용 프로그램에는 기본적인 오류 처리 기능이 포함되어 있습니다. 빈 검색 문자열을 검색합니다. 카운트할 수 있는 최대 단어 수나 줄 수를 초과하는 오류 등을 포함한 여러 다른 오류들을 처리하여 이 프로그램을 보다 강력하게 만들 수 있습니다.

참고 항목

작업

연습: Visual Basic으로 간단한 다중 스레드 구성 요소 만들기

방법: 이벤트 구독 및 구독 취소(C# 프로그래밍 가이드)

개념

스레딩(C# 및 Visual Basic)