연습: 다중 스레딩

업데이트: 2008년 7월

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

이 항목의 코드 예제를 빌드하려면

  1. 새 Visual Basic 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

    소스 파일

    두 번째 레이블

    Text

    Compare String

    세 번째 레이블

    Text

    Matching Words

    네 번째 레이블

    Text

    Lines Counted

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

  6. BackgroundWorker1 개체에 대한 다음 속성을 설정합니다.

    속성

    설정값

    WorkerReportsProgress

    True

    WorkerSupportsCancellation

    True

개별 스레드에서 실행되는 메서드를 정의하려면

  1. 프로젝트 메뉴에서 클래스 추가를 선택하여 프로젝트에 클래스를 추가합니다. 새 항목 추가 대화 상자가 표시됩니다.

  2. 템플릿 창에서 클래스를 선택하고 이름 필드에 Words.vb를 입력합니다.

  3. 추가를 클릭합니다. Words 클래스가 표시됩니다.

  4. Option Compare 문을 Class 문 위에 있는 Words 클래스의 맨 위에 추가합니다.

    Option Compare Text ' Case insensitive search.
    ' Use Option Compare Binary for case sensitive search.
    
  5. 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 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
    

스레드에서 이벤트를 처리하려면

  • 기본 폼에 다음 이벤트 처리기를 추가합니다.

    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
            MsgBox("Error: " & e.Error.Message)
        ElseIf e.Cancelled Then
            MsgBox("Word counting canceled.")
        Else
            MsgBox("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 event handler is called after the background thread
        ' reads a line from the source file.
        ' 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
    

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 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
    
  2. 해당 폼의 Start 단추에서 StartThread 메서드를 호출합니다.

    Private Sub Start_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Start.Click
    
        StartThread()
    End Sub
    

스레드를 중지하는 Cancel 단추를 구현하려면

  • Cancel 단추에 대한 Click 이벤트에서 StopThread 프로시저를 호출합니다.

    Private Sub Cancel_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Cancel.Click
    
        ' Cancel the asynchronous operation.
        Me.BackgroundWorker1.CancelAsync()
    End Sub
    

테스트

이제 응용 프로그램을 테스트하여 제대로 작동되는지 확인할 수 있습니다.

응용 프로그램을 테스트하려면

  1. F5 키를 눌러 응용 프로그램을 실행합니다.

  2. 폼이 표시되면 sourceFile 상자에 테스트하려는 파일의 파일 경로를 입력합니다. 예를 들어, 테스트 파일 이름이 Test.txt일 경우 C:\Test.txt라고 입력합니다.

  3. 두 번째 텍스트 상자에 텍스트 파일에서 응용 프로그램이 검색할 단어나 구를 입력합니다.

  4. Start 단추를 클릭합니다. LinesCounted 단추의 숫자가 즉시 증가됩니다. 이 작업이 끝나면 응용 프로그램은 "Finished Counting" 메시지를 표시합니다.

Cancel 단추를 테스트하려면

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

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

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

다음 단계

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

참고 항목

작업

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

기타 리소스

Visual Basic의 다중 스레딩

변경 기록

날짜

변경 내용

원인

2008년 7월

예제의 WordCount 메서드에서 오류가 수정되었습니다.

고객 의견