Vb.net 使用BackgroundWorker并更新UI

Vb.net 使用BackgroundWorker并更新UI,vb.net,Vb.net,这是我的代码,我想用它来更新用户界面,但我不知道怎么做。我正在寻找一个有评论的例子,以便我能理解它。到目前为止,我已经在一些网站上发布了帖子,每个人都会告诉我该做什么,或者给我举个例子,但是没有人会告诉我该用这个例子做什么!他们希望我知道代码的作用,但如果我知道它的作用,你真的认为我会问问题吗?无论如何,回到话题上来: 这就是我正在努力实现的目标。当我点击Leech_btn时,它会将我所有的网站从网站列表添加到一个名为“Array”的数组中。然后以数组作为参数运行backgroundworker

这是我的代码,我想用它来更新用户界面,但我不知道怎么做。我正在寻找一个有评论的例子,以便我能理解它。到目前为止,我已经在一些网站上发布了帖子,每个人都会告诉我该做什么,或者给我举个例子,但是没有人会告诉我该用这个例子做什么!他们希望我知道代码的作用,但如果我知道它的作用,你真的认为我会问问题吗?无论如何,回到话题上来:


这就是我正在努力实现的目标。当我点击Leech_btn时,它会将我所有的网站从网站列表添加到一个名为“Array”的数组中。然后以数组作为参数运行backgroundworker。后台工作人员从列表中下载网站字符串,并将其移到SourceDownloaded进行筛选。(它从网站上删除所有代理)然后它应该在代理列表中显示代理,但这就是我遇到的问题。我该怎么办?

如果我理解正确,问题就出在

Private _Proxies As New List(Of String)
Private _Array As New List(Of String)

Private Sub btnLeech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Leech_btn.Click
    Worker.RunWorkerAsync(_Array)
End Sub

'Open a bunch of new threads to download sources of webpages
Private Sub Fetch(ByVal sender As System.Object, _
                                 ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Worker.DoWork
    Dim websiteUri As Uri = Nothing
    Dim Website As String
    For I = 0 To _Array.ToList.Count - 1
        Website = _Array(I)
        Using wc As Net.WebClient = New Net.WebClient
            AddHandler wc.DownloadStringCompleted, AddressOf SourceDownloaded
            If Uri.TryCreate(Website, UriKind.Absolute, websiteUri) Then
                wc.DownloadStringAsync(New Uri(Website))
                Threading.Thread.Sleep(250)
            Else
                If Notify.Checked = True Then
                    TrayIcon.ShowBalloonTip(1, "Invalid website", "There was a invalid site in the list." & Website.ToString, ToolTipIcon.Error)
                    Me.TrashSite_list.Items.Add(Website)
                Else
                    Me.TrashSite_list.Items.Add(Website)
                End If
            End If
        End Using
    Next
End Sub
'Grab the proxies from the webpages
Private Sub SourceDownloaded(ByVal sender As Object, ByVal e As Net.DownloadStringCompletedEventArgs)
    Dim strRegex As String = "\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\:[0-9]{1,5}\b"
    Dim myRegexOptions As RegexOptions = RegexOptions.None
    Dim myRegex As New Regex(strRegex, myRegexOptions)
    Dim frm As New Proxies
    Dim i As Integer
    My.Settings.Proxies = New StringCollection
    If e.Error Is Nothing Then
        For Each myMatch As Match In myRegex.Matches(e.Result)
            If myMatch.Success Then
                Try
                    i += i
                    _Proxies.Add(myMatch.ToString)
                    Worker.ReportProgress(i)
                Catch ex As WebException
                    MessageBox.Show(
                        ex.ToString,
                        ErrorToString,
                        Windows.Forms.MessageBoxButtons.OK)
                End Try
            End If
        Next
    End If
End Sub

Private Sub Worker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles Worker.ProgressChanged
    Proxy_list.Items.AddRange(_Proxies.ToArray)
        Proxy2_lbl.Text = "Proxies: " & Proxy2_list.Items.Count
    Proxy_lbl.Text = "Proxies: " & Proxy2_list.Items.Count
End Sub
这是因为UI和worker在不同的线程中。您可以添加以下内容:

Dim Proxies() As String = My.Resources.SiteList6.Split(vbNewLine)
Proxy_list.Items.AddRange(Proxies)
在代码中,将“Proxy\u list.Items.AddRange(Proxies)”替换为


请参阅,一个类似的问题。

您的fetch函数接收一个字符串数组,但随后将e.参数转换为字符串。我敢肯定,当I=Array.length检查我的线程时,你在Website=Array(I)上也会遇到一个异常。现在,我添加了一个不同的方法来处理它。
Private Delegate Sub UpdateListDelegate(byval itemName() as string)
Private Sub UpdateList(byval itemName() as string)
    If Me.InvokeRequired Then
        Me.Invoke(New UpdateListDelegate(AddressOf UpdateList), itemName)
    Else
        ' UpdateList
        ' add list add code
        Proxy_list.Items.AddRange(itemName)
    End If
End Sub
 UpdateList(Proxies)