So I have this checklistbox which contains names and then I want to do something with these names. But at the moment it is doing the names one at a time, so I want to know how to get this multithreading properly working. This is in VB.net for your information. Here is the code of the single threading loop:
Dim selectedStreamers As New List(Of String)
For Each item In streamerList.CheckedItems
selectedStreamers.Add(item)
Next
For Each item In selectedStreamers
streamerOnline(item)
Select Case condition
Case StreamersCondition.Streaming
Me.Invoke(Sub() console.Text &= item + " is online." & vbNewLine)
Case StreamersCondition.Offline
Me.Invoke(Sub() console.Text &= item + " is offline." & vbNewLine)
Case StreamersCondition.UserNotFound
Me.Invoke(Sub() console.Text &= item + " hasn't been found." & vbNewLine)
End Select
Next
Now I tried Parallel.foreach and I can't seem to get it to work? Here is the code for that:
Parallel.ForEach(selectedStreamers, Sub(streamer)
streamerOnline(streamer)
System.Threading.Thread.Sleep(1000)
Select Case condition
Case StreamersCondition.Streaming
Me.Invoke(Sub() console.Text &= streamer + " is online." & vbNewLine)
Case StreamersCondition.Offline
Me.Invoke(Sub() console.Text &= streamer + " is offline." & vbNewLine)
Case StreamersCondition.UserNotFound
Me.Invoke(Sub() console.Text &= streamer + " hasn't been found." & vbNewLine)
End Select
End Sub)
Whats happening at the moment is that it will go through the first user and work perfectly, then when it gets to the second user the loop just doesn't allow the user to go to the streamerOnline(streamer) therefore it gives the user the same output as the previous user. Also, it freezes sometimes and doesn't allow me to exit. This can be fixed by solving this, but it would also be beneficial to have the loop in a new thread - so how would I do this?
Thanks for the feeback in advance.