我有一个简单的程序来检查网页的字符串,例如:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim urls() As String = TextBox1.Lines()
Dim links() As String = TextBox2.Lines()
For Each url As String In urls
CheckForLinks(url, links)
Next
End Sub
Private Sub CheckForLinks(ByVal url As String, ByVal links() As String)
Dim wc As New WebClient()
Dim source As String = wc.DownloadString(url)
'MessageBox.Show(source)
For Each link As String In links
If (source.IndexOf(link) <> -1) Then
TextBox3.AppendText("url: " + url + " link: " + link + vbCrLf)
Exit For
Else
TextBox3.AppendText("url: " + url + " link: " + "NOT FOUND" + vbCrLf)
End If
Next
End Sub
它工作正常,但速度很慢,因为它一次检查一个网页。
我意识到我可以在 button1_click 子中使用 parallel.for each,但我担心它可能会生成大量线程并使 Web 连接过载。
我希望能够设置它使用的确切线程数,但我不确定从哪里开始。例如,我如何将每个 url 分配给线程等。