次の方法で共有


チュートリアル : マルチスレッド

更新 : 2008 年 7 月

このチュートリアルでは、テキスト ファイルで語句を検索する、マルチスレッド アプリケーションの作成方法を説明します。次の方法を学ぶことができます。

  • BackgroundWorker コンポーネントから呼び出し可能なメソッドを含むクラスの定義。

  • BackgroundWorker コンポーネントで発生したイベントの処理。

  • メソッドを実行するための BackgroundWorker コンポーネントの開始。

  • BackgroundWorker コンポーネントを停止する Cancel ボタンの実装。

このトピックのコード例を作成するには

  1. Visual Basic の新しい Windows アプリケーション プロジェクトを開き、Form1 という名前のフォームを作成します。

  2. Form1 に 2 つのボタンと 4 つのテキスト ボックスを追加します。

  3. 次の表のとおりにオブジェクトに名前を付けます。

    オブジェクト

    プロパティ

    設定

    1 番目のボタン

    Name, Text

    Start、Start

    2 番目のボタン

    Name, Text

    Cancel、Cancel

    1 番目のテキスト ボックス

    Name, Text

    SourceFile、""

    2 番目のテキスト ボックス

    Name, Text

    CompareString、""

    3 番目のテキスト ボックス

    Name, Text

    WordsCounted、"0"

    4 番目のテキスト ボックス

    Name, Text

    LinesCounted、"0"

  4. 各テキスト ボックスの横にラベルを追加します。次の表に示すように、各ラベルの Text プロパティを設定します。

    オブジェクト

    プロパティ

    設定

    1 番目のラベル

    Text

    Source File

    2 番目のラベル

    Text

    Compare String

    3 番目のラベル

    Text

    Matching Words

    4 番目のラベル

    Text

    Lines Counted

  5. [ツールボックス] の [コンポーネント] にあるBackgroundWorker コンポーネントをフォームに追加します。追加したコンポーネントがフォームのコンポーネント トレイに表示されます。

  6. BackgroundWorker1 オブジェクトに対して以下のプロパティを設定します。

    プロパティ

    設定

    WorkerReportsProgress

    True

    WorkerSupportsCancellation

    True

個別のスレッドで動作するメソッドを定義するには

  1. [プロジェクト] メニューの [クラスの追加] を選択し、プロジェクトにクラスを追加します。[新しい項目の追加] ダイアログ ボックスが表示されます。

  2. テンプレート ウィンドウの [クラス] を選択し、名前フィールドに「Words.vb」と入力します。

  3. [追加] をクリックします。Words クラスが表示されます。

  4. Words クラスの一番上の Class ステートメントの上に、Option Compare ステートメントを追加します。

    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. 2 番目のテキスト ボックスに、アプリケーションがテキスト ファイル内を検索する語句またはフレーズを入力します。

  4. [Start] をクリックします。LinesCounted ボックスで、直ちにインクリメントが開始されます。完了すると、アプリケーションは "Finished Counting" というメッセージを表示します。

Cancel ボタンをテストするには

  1. F5 キーを押して、アプリケーションを開始します。前の手順で説明したように、ファイル名と検索語句を入力します。動作が完了する前にプロシージャをキャンセルするだけの時間があるように、サイズの大きいファイルを選択します。

  2. Start をクリックして、アプリケーションを起動します。

  3. [Cancel] をクリックします。アプリケーションは直ちにカウントを中止します。

次の手順

このアプリケーションには、基本的なエラー処理が含まれています。空白の検索文字列が検出されます。語句の数やカウントする行数が最大値を超えた場合など、その他のエラーを処理することで、このプログラムの信頼性をより高くできます。

参照

処理手順

チュートリアル : Visual Basic による簡単なマルチスレッド コンポーネントの作成

その他の技術情報

Visual Basic におけるマルチスレッド

変更履歴

日付

履歴

理由

2008 年 7 月

例の中の WordCount メソッドのエラーを修正

カスタマ フィードバック