0

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.

1ben99
  • 557
  • 1
  • 7
  • 14
  • Possible duplicate of [.net while crash when loose focus win7](https://stackoverflow.com/questions/45555204/net-while-crash-when-loose-focus-win7) (ignore the question and go right down to my answer, start at _**The basics**_) – Visual Vincent Oct 01 '17 at 06:04
  • Are you certain that this will be significantly quicker? It's a relatively tight loop and threading overheads can sometimes make the program run more slowly. Secondly, if you only have a couple of cores in the processor executing the threads, you wont get much of an improvement. Finally, if you don't have many users on line each time the loop is executed, you might not notice a difference anyway. – David Wilson Oct 01 '17 at 08:45
  • Each loop probably takes about 2-3 seconds on average, so I am testing with like 5 users, so when the loop is running single threaded it will take like 10 seconds minimum. So It will definitely be worth it in the long run. Also, the program will freeze if so I don't want it freezing for 10 seconds at all. – 1ben99 Oct 01 '17 at 11:33
  • Also, about the thread, if I were to make the thread and call it would it still do each parallel loop? because if I was running multiple threads it would get messed up because it is reading from the same list, so the threads would do the same thing? – 1ben99 Oct 01 '17 at 11:37
  • Running it as a For Each loop would mean that each iteration could happen in any order. If your users don't need to wait for the loop to finish, you could just run it on its own thread separate from the UI thread. – David Wilson Oct 11 '17 at 18:43
  • btw, if you add @davidwilson to the beginning of your comments, I'll be notified of it. :-) – David Wilson Oct 11 '17 at 18:44

0 Answers0