Search code examples
vb.netmultithreadingbackgroundworker

Turning For loop in Backgroundworker into multithreader


I have a background worker where I currently am running a setup to loop through and send the http commands to these devices to update their firmware. Currently it's very slow, looping though each one and sending it.

My first question is, how can I multithread this so increase the speed? Usually I would make a task per device, add to a task array, and do a "wait all". But the device numbers could get up to 300 here so I want to limit it to a smart amount and do it dynamically.

And my second question is, is it wise to do so in the back groundworker that is already a thread (so I can show the progress bar)? Or is there another way to do it that is smarter?

Here's the current setup:

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork

    'loop through devices
    For Each assignment As FirmwareAssign In assignList  
    'firmwareassign is a struct with credentials for http, ip address, and device brand as strings


        'Send Update Request
        Select Case assignment.devcebrand.tolower 
            Case "brand1"
                Logstatement += brand1.updateFirmware(assignment, assignment.deviceipaddress)
            Case "brand2"
                Logstatement += brand2.updateFirmware(assignment, assignment.deviceipaddress)
            Case "brand3"
                Logstatement += brand3.updateFirmware(assignment, assignment.deviceipaddress)
        End Select

     'don't worry, not real names of my classes
    Next
End Sub

These functions are network based. They will grab the new firmware image, and send it to the ip address over the network. So I also want to limit it since we don't want the bandwidth to go nuts. Then it will add it to the logstatement (a string) to display at the end for the user to know if something went wrong.


Solution

  • Since you are sending these across the network and your bottle-neck is likely the network, running multiple threads will not speed it up, if anything it will slow down.

    If this is not the case, or you want to test this anyway, here are your 2 answers:

    1: If you are already doing a collection, in this case a task array, your are almost there. What I do is stick another loop in your current loop and run while the array/collection length is greater that X (maybe 10 in your case) and in that loop, check the thread status and remove any of those that are complete. This will make it jump out of the inner loop and add more threads until you hit X again.

    2: While I don't think there is anything that prevents you from running threads from other threads, the logistics of getting updates back to the GUI might be a nightmare. I would spawn the threads from your main thread which makes progress updates real easy.