我想了解最好的方法來加速一個小程序,在多個網站的源搜索字符串。因爲它代表的程序如下:.net多線程webclient.DownloadString,或使用DownloadStringAsync?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim urls() As String = TextBox1.Lines()
Dim stringstofind() As String = TextBox2.Lines()
For Each url As String In urls
CheckForStrings(url, stringstofind)
Next
End Sub
Private Sub CheckForStrings(ByVal url As String, ByVal stringstofind() As String)
Dim wc As New WebClient()
Dim source As String = wc.DownloadString(url)
'MessageBox.Show(source)
For Each stringtofind As String In stringstofind
If (source.IndexOf(stringtofind) <> -1) Then
TextBox3.AppendText("url: " + url + " string: " + stringtofind + vbCrLf)
Exit For
Else
TextBox3.AppendText("url: " + url + " string: " + "NOT FOUND" + vbCrLf)
End If
Next
End Sub
,似乎可用的選項有: 線程最初使用每一個的Parallel.For每個循環。除了一些編輯以避免交叉線程問題和阻止GUI之外,它看起來很簡單,但似乎並不是實現它的最佳方式。
使用webclient.DownloadStringAsync方法。 這是我看到的第一件事,但我無法解決如何從DownloadStringCompleted事件返回結果字符串。 此外,如果我可以解決這個問題,你如何限制多少simultainious請求,以避免過載網絡連接?
我還介紹了使用.net4.5是很好看一些C#的例子,但該計劃將需要在運行SERVER2003,所以我想這就是出了問題
任何幫助非常感謝的。
我在TPL上並不強壯,但由於它是基於CPU分配的,因此不會處理不會對CPU造成負擔的慢速連接。一個線程池可以改變胎面池的大小,這可能是一種更好的方法。搜索所以我知道我以前見過這個問題。 – Paparazzi
這是一個使用WithDegreeOfParallelism進行調節的方法。 http://stackoverflow.com/questions/8869812/a-faster-way-to-download-multiple-files – Paparazzi
謝謝,這是相當涵蓋我在想什麼,與一些有用的額外建議。所以你認爲我應該去那個,而不是打擾與下載stringsAsync方法? – Steve