2012-10-30 61 views
0
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,但我不確定如何去做。我正在尋找一個評論的例子,所以我可以理解它。到目前爲止,我已經在幾個網站上發佈了,每個人都會告訴我該怎麼做,或給我一個例子,但沒有人會告訴我該怎麼做!他們希望我知道代碼的作用,但如果我知道它做了什麼,你真的認爲我會問問題嗎?無論如何,回到主題:使用BackgroundWorker和更新UI

這是我想要完成的。當我點擊Leech_btn時,它將我所有網站從Website_list添加到名爲「Array」的數組中。然後它以數組作爲參數運行背景工作。後臺工作人員從列表中下載網站的字符串並將其移至SourceDownloaded進行過濾。 (它從網站中刪除所有代理)然後它應該在Proxy_list中顯示代理,但這是我遇到問題的地方。我該怎麼辦?

+0

您的獲取函數接收一個字符串數組,但是當時然後將e.Argument作爲一個字符串進行投射。 我敢肯定,當我= Array.Length時,你也會在Website = Array(I)上得到一個異常 –

回答

0

,如果我理解正確,問題是在

Dim Proxies() As String = My.Resources.SiteList6.Split(vbNewLine) 
Proxy_list.Items.AddRange(Proxies) 

這是因爲在不同的線程的UI和工人。你可以補充一點:

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) 

替換 「Proxy_list.Items.AddRange(代理)」 見vb.net: listbox.items.add() throws exception in same class,類似的問題。

+0

現在檢查我的線程,我添加了一個不同的方法來處理它。 – Bizzet

相關問題