Click here to Skip to main content
15,891,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
What i want is to download multiple url webpages using multi-threading and also add the content from each page to a similar richtextbox. But iam ending up with only single thread content being added multiple times into the richtextbox. My code is :

C#
For i As Integer = 0 To ListBox1.Items.Count - 1
           dim t as new thread(addressof dwnld)
glblcnt = i
       Next


C#
Public Sub dwnld()

        Dim result As String = String.Empty
      
        Dim txt As String = ""

        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(ListBox1.Items(glblcnt).tostring, HttpWebRequest)

        request.Method = "GET"

        Using stream = request.GetResponse().GetResponseStream()
            Using reader = New StreamReader(stream, Encoding.UTF8)
                result = reader.ReadToEnd()
            End Using
        End Using

        Dim doc As New HtmlAgilityPack.HtmlDocument()

        doc.Load(New StringReader(result))

        Dim root As HtmlNode = doc.DocumentNode

        For Each link As HtmlNode In root.SelectNodes("//a")
            Dim att As String = link.InnerText.Trim
            txt = txt & att
        Next

        Me.settext(txt)

    End Sub



the problem is that i need a way to pass different url from a listbox to each thread to download a different web page. Then each webpage will undergo same processing and then the links to be added to a richtextbox.

info :

settext method is a delegate where thread adds the text to the richtextbox - no problem with that

If anyone could please let me know how to correct the problem i would be very thankful.

Thanks
Posted

1 solution

To pass data into the thread you need to do something similar

VB
for Counter as integer = 0 to 100
 dim t as new thread(addressof FooBar)
 t.start(counter)
next

private sub FooBar(byval Value as integer)
 debug.print(value.tostring)
end sub


use this article for further reading ParameterizedThreadStart Delegate[^]
 
Share this answer
 
Comments
amit_upadhyay 3-Nov-11 22:34pm    
damn. I asked on atleast 3 other forums with actual question of whether we can pass parameter to a function via thread and all i got was plain no. Thanks for the help and link. Much appreciated. Got it working :)
Simon_Whale 4-Nov-11 2:52am    
Your welcome :))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900