使用单独的线程将多个网页下载到一个公共的richtextbox [英] download multiple webpages using separate threads to a common richtextbox
问题描述
我想要的是使用多线程下载多个URL网页,并将每个页面的内容添加到类似的richtextbox.但是iam最后只将单线程内容多次添加到richtextbox中.我的代码是:
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
问题是我需要一种方法,将列表框中的不同url传递给每个线程,以下载不同的网页.然后,每个网页将经过相同的处理,然后将链接添加到Richtextbox.
信息:
settext方法是一个委托,其中线程将文本添加到richtextbox中-没问题
如果有人可以,请让我知道如何解决该问题,我将非常感激.
谢谢
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
推荐答案
要将数据传递到线程中,您需要执行类似的操作
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
使用本文进一步阅读 ParameterizedThreadStart委托 [ ^ ]
use this article for further reading ParameterizedThreadStart Delegate[^]
这篇关于使用单独的线程将多个网页下载到一个公共的richtextbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!