這是我的代碼: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的工作原理,但過得真慢..
Debug + Exceptions,勾選CLR異常的拋出框。請務必閱讀MSDN Library文檔,瞭解您可以在DoWork內執行哪些操作。更新ListBox不是其中之一。 –
如果我使用try catch ex作爲例外msgbox(ex ...)end試試我沒有收到錯誤。你能告訴我在哪裏可以打勾嗎? – Ruben
我在進行過程中完成了這段代碼:如果e.error不是沒有,那麼msgbox的錯誤。以及我得到這個錯誤:錯誤:跨線程操作無效:控制'combobox1'從一個線程訪問,而不是它創建的線程。 – Ruben