By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,571 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,571 IT Pros & Developers. It's quick & easy.

VB.net BackgroundWorker Updating UI

P: 3
Expand|Select|Wrap|Line Numbers
  1. Private _Proxies As New List(Of String)
  2.     Private _Array As New List(Of String)
  3.  
  4.     Private Sub btnLeech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Leech_btn.Click
  5.         Worker.RunWorkerAsync(_Array)
  6.     End Sub
  7.  
  8.     'Open a bunch of new threads to download sources of webpages
  9.     Private Sub Fetch(ByVal sender As System.Object, _
  10.                                      ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Worker.DoWork
  11.         Dim websiteUri As Uri = Nothing
  12.         Dim Website As String
  13.         For I = 0 To _Array.ToList.Count - 1
  14.             Website = _Array(I)
  15.             Using wc As Net.WebClient = New Net.WebClient
  16.                 AddHandler wc.DownloadStringCompleted, AddressOf SourceDownloaded
  17.                 If Uri.TryCreate(Website, UriKind.Absolute, websiteUri) Then
  18.                     wc.DownloadStringAsync(New Uri(Website))
  19.                     Threading.Thread.Sleep(250)
  20.                 Else
  21.                     If Notify.Checked = True Then
  22.                         TrayIcon.ShowBalloonTip(1, "Invalid website", "There was a invalid site in the list." & Website.ToString, ToolTipIcon.Error)
  23.                         Me.TrashSite_list.Items.Add(Website)
  24.                     Else
  25.                         Me.TrashSite_list.Items.Add(Website)
  26.                     End If
  27.                 End If
  28.             End Using
  29.         Next
  30.     End Sub
  31.     'Grab the proxies from the webpages
  32.     Private Sub SourceDownloaded(ByVal sender As Object, ByVal e As Net.DownloadStringCompletedEventArgs)
  33.         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"
  34.         Dim myRegexOptions As RegexOptions = RegexOptions.None
  35.         Dim myRegex As New Regex(strRegex, myRegexOptions)
  36.         Dim frm As New Proxies
  37.         Dim i As Integer
  38.         My.Settings.Proxies = New StringCollection
  39.         If e.Error Is Nothing Then
  40.             For Each myMatch As Match In myRegex.Matches(e.Result)
  41.                 If myMatch.Success Then
  42.                     Try
  43.                         i += i
  44.                         _Proxies.Add(myMatch.ToString)
  45.                         Worker.ReportProgress(i)
  46.                     Catch ex As WebException
  47.                         MessageBox.Show(
  48.                             ex.ToString,
  49.                             ErrorToString,
  50.                             Windows.Forms.MessageBoxButtons.OK)
  51.                     End Try
  52.                 End If
  53.             Next
  54.         End If
  55.     End Sub
  56.  
  57.     Private Sub Worker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles Worker.ProgressChanged
  58.         Proxy_list.Items.AddRange(_Proxies.ToArray)
  59.             Proxy2_lbl.Text = "Proxies: " & Proxy2_list.Items.Count
  60.         Proxy_lbl.Text = "Proxies: " & Proxy2_list.Items.Count
  61.     End Sub
  62.  
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 backgroundworker. The background worker downloads the string of a website from the list _Array and moves it over to SourceDownloaded 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?
Oct 30 '12 #1
Share this Question
Share on Google+
5 Replies


PsychoCoder
Expert Mod 100+
P: 465
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 :)
Oct 31 '12 #2

P: 3
@PsychoCoder
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.
Oct 31 '12 #3

PsychoCoder
Expert Mod 100+
P: 465
@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?
Nov 1 '12 #4

P: 3
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 backgroundworker 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.
Nov 1 '12 #5

Frinavale
Expert Mod 5K+
P: 9,731
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.BeginInvoke method to do the updation.

-Frinny
Nov 3 '12 #6

Post your reply

Sign in to post your reply or Sign up for a free account.