Multithreading with queue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • winzone
    New Member
    • Jan 2009
    • 10

    Multithreading with queue

    Hi All,

    I have more than 5 thread task that is dynamic number.

    I want to run 5 thread at a time.
    when one of the 5 thread is finished another one queue thread start to run.
    Then continue to run until all queue thread is finished.

    I have problem of how to queue the thread if over 5 thread.
    how to pass the parameter? sPar.Item(iIndx ).ToString()

    Below is my code.

    Code:
     Private Const MAX_THREADS As Integer = 7
        Private threads(MAX_THREADS) As Thread
    Private Sub btnRunThread_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnRunThread.Click
            Dim cThread As New ThreadClass
            Dim rnd As New Random
            
            For i As Integer = 0 To MAX_THREADS
                cThread.sPar.Add(i.ToString())
                If i > 4 Then
                    threads(i) = New Thread(AddressOf cThread.ThreadMethod1)
                    cThread.iIndx = i
                    threads(i).Start()
                    threads(i).Join()
                Else
                    threads(i) = New Thread(AddressOf cThread.ThreadMethod1)
                    cThread.iIndx = i
                    threads(i).Start()
                End If
            Next
    End Sub
    
    Public Class ThreadClass
        Public sPar As New ArrayList
        Public iIndx As Integer
        
        Public Sub ThreadMethod1()
            Thread.Sleep(200)
            For i As Integer = 1 To 100
                Console.WriteLine("Thread " & sPar.Item(iIndx).ToString() & ": " & i)
            Next
        End Sub
    End Class
    Is there any solution or advice, please guide me.
Working...