Question Threading Provides Duplicates in ListView

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
Hey everyone, I really hope someone can help me since I've been struggling to get this. Here's what I'm trying to do..

I have a list of items that I want to put into a queue and then process using either multiple threads or actions. Once I've processed the item I then want to add it along with its result into a ListView. I obviously do not want to get duplicates or have items be skipped.

Below is some of the code I've been messing with. I've struggled to get a thread pool working and found an example using ConcurrentQueue and Action via Microsoft, so that's what I've included. This works, but the results contain duplicates and even some missing data.. Any help would be really appriciated!

VB.NET:
Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
            'create and add to queue
            Dim cq As New ConcurrentQueue(Of String)()

            For Each s As String In mainlist
                cq.Enqueue(s)
            Next
            lblTotalCnt.Text = cq.Count

            'Action to consume the ConcurrentQueue
            Dim action As Action =
          Sub()
              For Each item As String In cq
                  While cq.TryDequeue(item)
                      'Processing item and adding it to ListView here
                      Try
                          testString = item
                          If PerformCheck(testString) = True Then
                              Dim str(2) As String
                              Dim itm As ListViewItem
                              str(0) = testString
                              str(1) = testUpdated

                              itm = New ListViewItem(str)

                              lstDetails.Items.Add(itm)
                              lblAnalyzedCnt.Text = lblAnalyzedCnt.Text + 1
                          Else
                              lblErrorsCnt.Text = lblErrorsCnt.Text + 1
                          End If
                      Catch ex As Exception
                          'Handle exception here
                      End Try
                  End While
              Next
          End Sub

            'Start 4 concurrent consuming actions
            Parallel.Invoke(action, action, action, action)
End Sub
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,350
Location
Sydney, Australia
Programming Experience
10+
Have you actually debugged the code? Particularly when using multiple threads, you can expect to solve many issues simply by reading the code. You need to execute it and watch it in action. Stepping through code in the debugger when multiple threads are executing can be confusing, because it will jump from thread to thread. It's often a good idea to add tracing code that includes the thread ID so you can see what threads did what and in what order.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,350
Location
Sydney, Australia
Programming Experience
10+
OK, I think I just noticed the issue:
For Each item As String In cq
    While cq.TryDequeue(item)

Is that really what you want? Think about it. Let's say that you had a production line in a factory and you had four workers. Let's say that there is a conveyor belt that items are fed on and the workers are supposed to pack those items. What would they do? Each worker would just keep taking items and packing them as long as there were items on the conveyor, right? That's each worker running a While loop. Would those workers do anything that corresponds to your For Each loop? Would any of the workers do something for every item on that conveyor belt?
 
Top Bottom