Create Multiple Background workers with HttpWebRequest

simpleonline

Active member
Joined
Sep 13, 2011
Messages
33
Programming Experience
Beginner
Okay so I've dug around all day and found that using multiple "background workers" would do what I would need for my application.

How can I create 4 background workers and then feed the urls to the webrequest from a listbox?

VB.NET:
Dim request As HttpWebRequest = HttpWebRequest.Create(URLGOESERE)                 
Dim response As HttpWebResponse                 
response = CType(request.GetResponse, HttpWebResponse)                 
Dim webstream As Stream = response.GetResponseStream                 
Dim streamreader As New StreamReader(webstream, Encoding.UTF8)                 
Dim html As String = streamreader.ReadToEnd                 
Messagebox.show(html.tostring)

I'm basically needing to have each webrequest access the listbox, grab a URL, process the webrequest and then go grab the next url on file.

I am trying to have that running 4x at a time until the sites are all looped through.

I've read a background worker is good vs. multi threading because it's easier to code.
 
A BackgroundWorker is not an alternative to multi-threading. It's one way to implement multi-threading. The DoWork event handler is executed on a secondary thread while the UI thread remains free to process UI messages. That's multiple threads, hence it's multi-threading. Creating multiple BackgroundWorkers is rarely a good idea and it's not here either. You would either use the ThreadPool class directly and use synchronous methods as you are or else you would use asynchronous methods. I say "directly" because BackgroundWorkers and asynchronous methods both use the the ThreadPool internally. Here's a bare bones example of using the ThreadPool to process each item in a ListBox and then update the UI after each one:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ProgressBar1.Value = 0
    ProgressBar1.Maximum = ListBox1.Items.Count

    For Each item In ListBox1.Items
        ThreadPool.QueueUserWorkItem(AddressOf ProcessItem, item)
    Next
End Sub

Private Sub ProcessItem(item As Object)
    'Process item here.

    UpdateUI()
End Sub

Private Sub UpdateUI()
    If InvokeRequired Then
        Invoke(New MethodInvoker(AddressOf UpdateUI))
    Else
        ProgressBar1.PerformStep()
    End If
End Sub
 
Back
Top