Results 1 to 29 of 29

Thread: Adding a For statement into a Backgroundworker

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2011
    Posts
    19

    Question Adding a For statement into a Backgroundworker

    Alright everyone, I have fixed the Invalid URI problem by using Uri.TryCreate

    Code:
    If Uri.TryCreate(Website, UriKind.Absolute, websiteUri) Then
                        wc.DownloadStringAsync(New Uri(Website))
                    Else
                            Me.TrashSite_list.Items.Add(Website)
                        End If
    And I have also fixed the Not Responding error, just a little bit, by adding
    Code:
     T = New Thread(New ThreadStart(AddressOf Fetch))
                    T.Start()
                    CheckForIllegalCrossThreadCalls = False
    Everyone is telling me this is a really bad way to do it and implanting my code into a background worker will make my program work much better. However, I have tried and tried, searched and searched, and even asked but nobody is willing to provide me with an example for what I need. What I need is to add a Background worker for the following code:

    #Region "Leech Proxies"
    Dim T As New System.Threading.Thread(AddressOf Fetch)
    'Leech Button
    Private Sub btnLeech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Leech_btn.Click
    If Mini.Checked = False Then
    If Website_list.Items.Count > 0 Then
    T = New Thread(New ThreadStart(AddressOf Fetch))
    T.Start()
    CheckForIllegalCrossThreadCalls = False
    Checker.Enabled = False
    Home.Enabled = False
    End If
    Else
    If Website_list.Items.Count > 0 Then
    Proxies.Show()
    Me.WindowState = FormWindowState.Minimized
    Me.Visible = False
    T = New Thread(New ThreadStart(AddressOf Fetch))
    T.Start()
    CheckForIllegalCrossThreadCalls = False
    End If
    End If
    End Sub

    'Open a bunch of new threads to download sources of webpages
    Private Sub Fetch()
    Dim Website As String
    Dim websiteUri As Uri = Nothing

    For I As Integer = 0 To Website_list.Items.Count - 1
    SyncLock Me.Website_list.Items
    Website = Me.Website_list.Items.Item(I)
    End SyncLock
    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))
    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
    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
    Proxy2_list.Items.Add(myMatch.ToString)
    Proxy_list.Items.Add(myMatch.ToString)
    Proxy2_lbl.Text = "Proxies: " & Proxy2_list.Items.Count
    Proxy_lbl.Text = "Proxies: " & Proxy2_list.Items.Count
    My.Settings.Proxies.Add(myMatch.ToString)
    Catch ex As WebException
    MessageBox.Show(
    ex.ToString,
    ErrorToString,
    Windows.Forms.MessageBoxButtons.OK)
    End Try
    End If
    Next
    End If

    End Sub

    #End Region
    If anyone is willing to possibly MSN or TeamViewer with me to help me solve my problem, shoot me a PM.
    If you want to solve it on your own, then here is the full source: http://tny.cz/f0c1717f
    If you want to post an example of what I need to do, please do so!
    Last edited by Bizzet; Oct 29th, 2012 at 06:32 PM. Reason: Update

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width