Click here to Skip to main content
13,000,742 members (57,826 online)
Rate this:
Please Sign up or sign in to vote.
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 :

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

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
    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.

Posted 2-Nov-11 22:32pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

 for Counter as integer = 0 to 100
  dim t as new thread(addressof FooBar)
 private sub FooBar(byval Value as integer)
 end sub

use this article for further reading ParameterizedThreadStart Delegate[^]
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170624.1 | Last Updated 3 Nov 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100