开发者

Backgroundworker doesn't work... VB.Net

开发者 https://www.devze.com 2023-03-30 03:57 出处:网络
this is my code: Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

this is my code:

    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

and combobox1.text = www.google.nl ( example )

at button 1 the code is:

BackgroundWorker1.RunWorkerAsync()

and if backgroundworker is done:

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

if i click button 1, i get within a half s开发者_StackOverflow中文版econd the message: Done

what's wrong with the code??

if i put the code inside backgroundworker1 just in button1 it works but goes really slow..


You can only update the UI from within the main application thread, in this case you're attempting to do it via a background thread that has been created by the background worker which will throw an exception as you've found.

What you'll need to do it run the code which adds to the ListBox on the main thread which you can do via BeginInvoke and a custom delegate which takes the item you want to add as a parameter, the delegate can then add the item to list box - there's an example of how to do this in the docs for BeginInvoke.


I would return a list or array of items to be added from the background worker and then fill the ListBox in the RunWorkerCompleted event handler.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号