Results 1 to 3 of 3

Thread: Threading Provides Duplicates in ListView

  1. #1
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2012
    Posts
    138
    Reputation
    74

    Threading Provides Duplicates in ListView

    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!

    Code:
    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 by digitaldrew; 06-20-2018 at 7:13 PM.

  2. #2
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,967
    Reputation
    1725
    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.

  3. #3
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,967
    Reputation
    1725
    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?

Similar Threads

  1. second time it duplicates the datagridview
    By Opticknerve in forum OLEDB
    Replies: 1
    Last Post: 07-13-2014, 5:52 AM
  2. Question Duplicates in DatagridView
    By vinelectronics in forum VB.NET General Discussion
    Replies: 0
    Last Post: 01-07-2010, 8:08 AM
  3. Duplicates Tables???
    By gate7cy in forum MS Access
    Replies: 0
    Last Post: 09-01-2009, 5:29 AM
  4. Determine Duplicates via LINQ
    By jsurpless in forum LINQ
    Replies: 0
    Last Post: 07-29-2009, 1:17 PM
  5. Checking duplicates
    By Elbob in forum Database General Discussion
    Replies: 12
    Last Post: 05-12-2009, 9:16 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •