Results 1 to 3 of 3

Thread: multiple threads selecting the same listbox item

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    467

    multiple threads selecting the same listbox item

    Hi, ok i have multiple threads running at once. All threads have the same code just chnaged a little for each one.

    Anyways what happens is when i thread runs it start as listbox index 0, gets the item then runs the rest of the code and also does index +1

    The other threads should use index 1, 2, 3 ect..., this works however sometimes 2 threads will end up with the same item, like if both are run at the same time, both threads use the same item.

    I can not find a way so that all threads use a different item in the list.

    How would this be done ?

    Thanks

  2. #2

    Re: multiple threads selecting the same listbox item

    Can I ask for a little background? There might be a better way to go about this than, say, accessing a GUI control.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: multiple threads selecting the same listbox item

    Put the objects into a queue first and then let each thread dequeue an item to use. If you were using .NET 4.0 or later, the obvious choice would be the ConcurrentQueue(Of T), which is inherently thread-safe. As you're using .NET 3.5 or earlier, you should use a Queue(Of T) and synchronise the dequeuing yourself, e.g.
    Code:
    Private ReadOnly items As New Queue(Of Object)
    
    Private ReadOnly syncRoot As New Object
    
    Private Function GetNextItem() As Object
        SyncLock syncRoot
            Return items.Dequeue()
        End SyncLock
    End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width