i have this code that is suppose to work with at starting the minimum number of threads, say 5. when it starts the first five threads, it automatically replaces any thread that is finished making sure that there is always five threads working.

the problem here is that, am finding it difficult to manage where am suppose perform locks

Private Sub StartProcess()
        intNextItemIndex = intMaxThreadCount
        ' start the first five threads
        For i = 0 To intMaxThreadCount
            If i < ListWorkItems.Count Then
            End If
        ' update running threads
    End Sub

    Private Sub StartNextItem(ByVal index As Integer)
        Dim tt As New ThreadTask
        If index < ListWorkItems.Count Then

            ' start parsing thread
            Dim t As New Thread(AddressOf DoWork)
            With tt
                .Index = index
                .Task = "count"
                .Data = CStr(index * index)
            End With
            With t
                .IsBackground = True
                .Name = "Thread " & index
            End With
        End If
    End Sub

    Private Sub DoWork(ByVal data As Object)
        Dim tt As ThreadTask = CType(data, ThreadTask)
        Dim sTemp As String = Nothing
        Dim objLock As Object = New Object()
        Dim t As Double = 0
        myWatch = New Stopwatch
            Debug.Print("Incoming index: " & CInt(tt.Index))

                    SyncLock (objLock)
                        sTemp = PerformWork(ListWorkItems(CInt(tt.Index)).ToString)
                        'sTemp = CountTo()
                    End SyncLock

            t = myWatch.ElapsedMilliseconds / 1000
            AddItem(tt.Index, t.ToString & " = " & sTemp)
            'AddItem(index, t.ToString)
        Catch ex As Exception
            ' catch error
            Debug.Print( ex.Message)
        End Try
 end sub

Public Sub AddItem(ByVal index As Integer, ByVal item As String)

    If Me.InvokeRequired Then
        Me.Invoke(New AddItemDelegate(AddressOf AddItem), index, item)
        Dim objLock As Object = New Object()
        Dim lvItem As New ListViewItem

        SyncLock (objLock)
            count += 1
            intNextItemIndex += 1
        End SyncLock

        With lvItem
            .Text = index.ToString
            If item.Length < 100 Then
            End If
        End With

        If count = ListWorkItems.Count Then
            Button1.Enabled = True
            tsslStatus.Text = "Finished!!"
            Debug.Print("Finished: " & index.ToString)
            tsslStatus.Text = "Processing item (" & count + 1 & ")"
        End If

    End If

End Sub

can someone help me review it and tell me whats wrong with it?

Are the loks ok?

Is this way of doing it ok?

What possible improvements can i make

thanks all


1 Answer 1


Unfortunately, the whole approach is broken. You should make your parallel tasks explicit in the code so that you don’t need cross-thread communication any more. Secondly, you are writing your own thread scheduler here. – Don’t. Schedulers already exist, take advantage of that.

Have a look at the Task Parallel Library, in particular the Task and TaskScheduler classes.

Better yet, try to model your algorithm to benefit from data parallelism and eschew explicit threading completely in favour of the automatically parallelised computations.

  • \$\begingroup\$ am using .net 2.0, of which the task Parallel is not available \$\endgroup\$
    – Smith
    Commented Jun 17, 2011 at 11:44
  • \$\begingroup\$ @Smith That’s a problem. Incidentally, why are you using a completely outdated (> 5 years old, > 2 versions outdated) version of .NET? \$\endgroup\$ Commented Jun 17, 2011 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.