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
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.
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