Condividi tramite


Procedura dettagliata: multithreading

Aggiornamento: Luglio 2008

In questa procedura dettagliata viene illustrata la creazione di un'applicazione con multithreading che cerca occorrenze di una parola in un file di testo. Vengono illustrate le funzionalità riportate di seguito.

  • Definizione di una classe con un metodo che può essere chiamato dal componente BackgroundWorker.

  • Gestione di eventi generati dal componente BackgroundWorker.

  • Avvio di un componente BackgroundWorker per l'esecuzione di un metodo.

  • Implementazione di un pulsante Cancel che consente di arrestare il componente BackgroundWorker.

Per compilare un esempio di codice per questo argomento

  1. Aprire un nuovo progetto Applicazione Windows di Visual Basic e creare un form denominato Form1.

  2. Aggiungere due pulsanti e quattro caselle di testo a Form1.

  3. Assegnare un nome agli oggetti, come illustrato nella tabella riportata di seguito.

    Oggetto

    Proprietà

    Impostazione

    Primo pulsante

    Name, Text

    Start, Start

    Secondo pulsante

    Name, Text

    Cancel, Cancel

    Prima casella di testo

    Name, Text

    SourceFile, ""

    Seconda casella di testo

    Name, Text

    CompareString, ""

    Terza casella di testo

    Name, Text

    WordsCounted, "0"

    Quarta casella di testo

    Name, Text

    LinesCounted, "0"

  4. Aggiungere un'etichetta accanto a ogni casella di testo. Impostare la proprietà Text per ogni casella, come illustrato nella tabella riportata di seguito.

    Oggetto

    Proprietà

    Impostazione

    Prima etichetta

    Text

    Source File

    Seconda etichetta

    Text

    Compare String

    Terza etichetta

    Text

    Matching Words

    Quarta etichetta

    Text

    Lines Counted

  5. Aggiungere al form un componente BackgroundWorker dalla sezione Componenti della Casella degli strumenti. Il componente verrà visualizzato nella barra componenti del form.

  6. Impostare le seguenti proprietà per l'oggetto BackgroundWorker1.

    Proprietà

    Impostazione

    WorkerReportsProgress

    True

    WorkerSupportsCancellation

    True

Per definire il metodo che verrà eseguito in un thread distinto

  1. Scegliere Aggiungi classe dal menu Progetto per aggiungere una classe al progetto. Verrà visualizzata la finestra di dialogo Aggiungi nuovo elemento.

  2. Selezionare Classe nella finestra dei modelli e digitare Words.vb nel campo del nome.

  3. Fare clic su Aggiungi. Verrà visualizzata la classe Words.

  4. Aggiungere un'istruzione Option Compare all'inizio della classe Words, prima dell'istruzione Class:

    Option Compare Text ' Case insensitive search.
    ' Use Option Compare Binary for case sensitive search.
    
  5. Aggiungere il codice seguente alla classe 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
    

Per gestire eventi dal thread

  • Aggiungere i gestori eventi seguenti al form principale:

    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
    

Per avviare e chiamare un nuovo thread ed eseguire il metodo WordCount

  1. Aggiungere le seguenti routine al programma:

    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. Chiamare il metodo StartThread dal pulsante Start nel form:

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

Per implementare un pulsante Cancel e arrestare il thread

  • Chiamare la routine StopThread dal gestore dell'evento Click per il pulsante Cancel.

    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
    

Test

È ora possibile verificare l'applicazione per assicurarsi che funzioni correttamente.

Per eseguire il test dell'applicazione

  1. Premere F5 per eseguire l'applicazione.

  2. Quando verrà visualizzato il form, immettere il percorso del file per cui si desidera eseguire la verifica nella casella sourceFile. Se ad esempio il file da verificare è denominato Test.txt, immettere C:\Test.txt.

  3. Nella seconda casella di testo immettere una parola o una frase da cercare nel file di testo.

  4. Fare clic sul pulsante Start. L'incremento del pulsante LinesCounted dovrebbe iniziare immediatamente. Al termine dell'operazione verrà visualizzato un messaggio per informare che il conteggio è terminato.

Per eseguire il test del pulsante Cancel

  1. Premere F5 per avviare l'applicazione e immettere il nome del file e la parola da cercare, come descritto nella routine precedente. Assicurarsi che il file selezionato abbia una dimensione sufficiente per consentire di annullare la routine prima del completamento.

  2. Fare clic sul pulsante Start per avviare l'applicazione.

  3. Fare clic sul pulsante Cancel. Il conteggio dovrebbe arrestarsi immediatamente.

Passaggi successivi

Nell'applicazione sono disponibili alcune funzionalità di base per la gestione degli errori, Individua stringhe di ricerca vuote. È possibile rendere più affidabile il programma tramite la gestione di altri errori, quale il superamento del numero massimo di parole o righe di cui è consentito il conteggio.

Vedere anche

Attività

Procedura dettagliata: modifica di componenti multithreading semplici con Visual Basic

Altre risorse

Multithreading in Visual Basic

Cronologia delle modifiche

Date

History

Motivo

Luglio 2008

Corretti errori nel metodo WordCount dell'esempio.

Commenti e suggerimenti dei clienti.