Click here to Skip to main content
Rate this: bad
good
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
        Next
 
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 2-Nov-11 23:32pm

1 solution

Rate this: bad
good
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)
  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[^]
  Permalink  
Comments
amit_upadhyay at 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 at 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
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 120
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 3 Nov 2011
Copyright © CodeProject, 1999-2014
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