VB.net BackgroundWorker Updating UI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bizzet
    New Member
    • Oct 2012
    • 3

    VB.net BackgroundWorker Updating UI

    Code:
    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
    This is my code and I want it to update the UI with it but I am unsure how to do so. I am looking for an example with commenting so I can understand it. So far I have posted on a few sites and everyone will tell me what to do or give me an example but nobody will tell me what to do with the example! They expect me to know what the code does, but If I knew what it did, do you really think I'd be asking questions? Anyways, back on topic:

    This is what I am trying to accomplish. When I click Leech_btn, it starts my backgroundworke r. The background worker downloads the string of a website from the list _Array and moves it over to SourceDownloade d to be filtered. (It removes all the proxies from the website) It then should display the proxies in Proxy_list but this is where I am having trouble. What should I do?
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    So what you're wanting is for someone to just do it for you, and add comments for you? Am I understanding you correctly? If not then please let me know :)

    Comment

    • Bizzet
      New Member
      • Oct 2012
      • 3

      #3
      Originally posted by PsychoCoder
      So what you're wanting is for someone to just do it for you, and add comments for you? Am I understanding you correctly? If not then please let me know :)
      No, I'm looking for someone to spend some actual time responding to my questions and explaining their reasoning and how I should more specifically go about it but I guess I can't expect that here. Especially when this is the first reply and your a mod. Ha.

      Comment

      • PsychoCoder
        Recognized Expert Contributor
        • Jul 2010
        • 465

        #4
        @Bizzet you're absolutely right, I was having a hard day and took it out on you and for this I apologize. As for your code, did you write that code or was it copied from somewhere (not meant as an insult I promise), just trying to figure out what we can do to help.

        Does that code do what you're looking for? If not what does it do versus what you're looking to accomplish?

        Comment

        • Bizzet
          New Member
          • Oct 2012
          • 3

          #5
          Well it was, but I have modified it so much that it's not really the same code as the original source anymore. I believe the backgroundworke r works as it is suppose to but my UI does get updated. It is suppose to be adding the proxies scraped from the website to my listbox, _Proxylist.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            What kind of application are you developing?

            If it's WPF, then you should be looking into using the Dispatcher class to update your UI with the stuff you retrieved in your threads.

            If it's a win forms application, you'll have to put a sync lock around the code that updates the control and use the ListView.BeginI nvoke method to do the updation.

            -Frinny
            Last edited by Frinavale; Nov 3 '12, 03:08 AM.

            Comment

            Working...