Question How do I add Thread Pool to this?

Tonygiulks

New member
Joined
Mar 8, 2009
Messages
1
Programming Experience
Beginner
I have a code that basically grab data from a file, split into array and use a FOR loop to input data into a different file. I want to use thread pool to create multiple threads to do a specific task. For example for the array() I want a thread to read the first item in a array and perform the task, a second thread reads the second item and perform the task, a third thread reads the third item and perform the task, etc. How would I add thread pool to something like:


VB.NET:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim data As String
        Dim arykeywords() As String
        Dim zz As Integer

        Dim FILE_NAME As String = "C:\test.txt"
        Dim objReader As New System.IO.StreamReader(FILE_NAME)
        Data = objReader.ReadToEnd
        objReader.Close()

        arykeywords = data.Split(Environment.NewLine)

        For zz = 0 To UBound(arykeywords)

            Dim line As String = arykeywords(zz)

            Dim OTHER_FILE As String = "C:\test2.txt"
            If System.IO.File.Exists(OTHER_FILE) = True Then
                Dim objWriter As New System.IO.StreamWriter(OTHER_FILE)
                objWriter.Write(line)
                objWriter.Close()
                MsgBox("Text written to file")
            Else
                MsgBox("File Does Not Exist")
            End If
        Next zz
    End Sub
End Class


Would it be:
VB.NET:
Imports System.Net
Imports System.Threading
Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ThreadPool.QueueUserWorkItem(AddressOf Initialize)

    End Sub
Private Sub Initialize(ByVal State As Object)

        Dim data As String
        Dim arykeywords() As String
        Dim zz As Integer

        Dim FILE_NAME As String = "C:\test.txt"
        Dim objReader As New System.IO.StreamReader(FILE_NAME)
        Data = objReader.ReadToEnd
        objReader.Close()

        arykeywords = data.Split(Environment.NewLine)

        For zz = 0 To UBound(arykeywords)

            Dim line As String = arykeywords(zz)

            Dim OTHER_FILE As String = "C:\test2.txt"
            If System.IO.File.Exists(OTHER_FILE) = True Then
                Dim objWriter As New System.IO.StreamWriter(OTHER_FILE)
                objWriter.Write(line)
                objWriter.Close()
                MsgBox("Text written to file")
            Else
                MsgBox("File Does Not Exist")
            End If
        Next zz
    End Sub
End Class
Or is I'm far off? I would really appreciate any help with this.
 
There is some good reading here. Many things to consider with threads.

VB.NET:
Dim td As Thread
td = New Thread(Addressof Intialize)
td.Start()
 
Back
Top