2011-08-21 70 views
2

這是我的代碼:BackgroundWorker的不工作... VB.Net

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 
    For i = 0 To 1000 
     Dim inum As String = i & "0" 


     Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://www.google.nl/search?q=site:" & combobox1.Text & "&hl=nl&start=" & inum) 
     Dim response As System.Net.HttpWebResponse = request.GetResponse 

     Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream()) 

     Dim sourcecode As String = sr.ReadToEnd 
     Dim search As String = combobox1.Text 
     Dim r As New System.Text.RegularExpressions.Regex("http://" & search & "/\w*") 
     Dim matches As MatchCollection = r.Matches(sourcecode) 


     For Each itemcode As Match In matches 

      Dim item As String = (itemcode.ToString.Split("""").GetValue(0)) 
      Dim url As New Net.WebClient 
      Dim str As String = url.DownloadString("http://www.prcheck.nl/results.php?url=" & item) 

      If str.Contains(">0/10") Then 
       ListBox1.Items.Add("(0/10) " & item) 
      ElseIf str.Contains("1/10") Then 
       ListBox1.Items.Add("(1/10) " & item) 
      ElseIf str.Contains("2/10") Then 
       ListBox1.Items.Add("(2/10) " & item) 
      ElseIf str.Contains("3/10") Then 
       ListBox1.Items.Add("(3/10) " & item) 
      ElseIf str.Contains("4/10") Then 
       ListBox1.Items.Add("(4/10) " & item) 
      ElseIf str.Contains("5/10") Then 
       ListBox1.Items.Add("(5/10) " & item) 
      ElseIf str.Contains("6/10") Then 
       ListBox1.Items.Add("(6/10) " & item) 
      ElseIf str.Contains("7/10") Then 
       ListBox1.Items.Add("(7/10) " & item) 
      ElseIf str.Contains("8/10") Then 
       ListBox1.Items.Add("(8/10) " & item) 
      ElseIf str.Contains("9/10") Then 
       ListBox1.Items.Add("(9/10) " & item) 
      ElseIf str.Contains("10/10") Then 
       ListBox1.Items.Add("(10/10) " & item) 
      Else 
       ListBox1.Items.Add("(0/10) " & item) 

      End If 

      Label2.Text = ListBox1.Items.Count 
     Next 



     If Not sourcecode.Contains("<b>Volgende</b>") Then 
      MsgBox("") 
      Exit For 
     End If 
    Next 
End Sub 

和combobox1.text = www.google.nl(例如)

在按鈕1中的代碼是:

BackgroundWorker1.RunWorkerAsync() 

,如果BackgroundWorker的完成:

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted 
    MsgBox("Done") 
End Sub 

如果我點擊按鈕1,我會在半秒內收到消息:完成

代碼有什麼問題?

如果我把裏面的代碼backgroundworker1只是在Button1的工作原理,但過得真慢..

+1

Debug + Exceptions,勾選CLR異常的拋出框。請務必閱讀MSDN Library文檔,瞭解您可以在DoWork內執行哪些操作。更新ListBox不是其中之一。 –

+0

如果我使用try catch ex作爲例外msgbox(ex ...)end試試我沒有收到錯誤。你能告訴我在哪裏可以打勾嗎? – Ruben

+0

我在進行過程中完成了這段代碼:如果e.error不是沒有,那麼msgbox的錯誤。以及我得到這個錯誤:錯誤:跨線程操作無效:控制'combobox1'從一個線程訪問,而不是它創建的線程。 – Ruben

回答

3

只能從主應用程序線程中更新UI,在這種情況下,你試圖通過做一個由後臺工作人員創建的後臺線程,它會在您發現的時候拋出異常。

你需要做的運行,這增加了主線程的列表框,您可以通過BeginInvoke做和自定義委託這需要要添加作爲參數,委託可以再該項目的代碼是什麼將項目添加到列表框 - 有一個如何在BeginInvoke文檔中執行此操作的示例。

+0

但我不明白,錯誤與列表框沒有任何關係?錯誤是爲組合框... – Ruben

0

我會返回要從後臺工作者添加的項目列表或數組,然後在RunWorkerCompleted事件處理程序中填充ListBox。

+0

這是行不通的。 – Ruben