473,382 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

VB.net BackgroundWorker Updating UI

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
5 4009
PsychoCoder
465 Expert Mod 256MB
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
Bizzet
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
465 Expert Mod 256MB
@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
Bizzet
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
9,735 Expert Mod 8TB
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

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

Similar topics

3
by: Richard Bysouth | last post by:
Hi In my app I'm using a BackgroundWorker component to execute a long-running task - updating a progress on my form fine. However, I'd like to provide more information to that form while the...
3
by: Jakanapes | last post by:
I'm trying to write my first program using the backgroundworker and am looking for some example code. I kow that you can use the UserState member to pass more information back to the UI for...
5
by: Beorne | last post by:
Hello, I'm a Java programmer and I'm new to C# (I've read something but I've no real programming experience). I'm programming a form that displays some data from an external source (i.e. a com...
1
by: planetthoughtful | last post by:
Hi All, I have a Windows Form app that does some work in a BackgroundWorker. I'd like to append information to a textbox from within the BackgroundWorker but it keeps telling me that I can't do...
14
by: =?Utf-8?B?SXNobWFlbA==?= | last post by:
Hi, I have a form with a progress bar on it and wanted to use the BackgroundWorker to be able to update the progress. I looked at examples, run some of them, but in debug, when the code gets to...
1
by: Manikandan | last post by:
Hi, I have a form with progress bar. I'm querying sql server with large number of records(around 1 lakh) for export The records are written to a text file I'm showing the process running by...
9
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
13
by: Johnny Jörgensen | last post by:
Hi I have a procudure that takes some time and thus slows down the main system. I want to put it in a backgroundworker component to run it asynchroneously. But in the procedure, I want to update a...
1
by: K Viltersten | last post by:
> bar.PerformStep (); I noticed that there's a property for the bar stating what time it should take for the animation to move the fill to the requested spot. I'm guessing that's the time i...
4
by: Edwin Velez | last post by:
http://msdn.microsoft.com/en-us/library/806sc8c5.aspx The URL above gives sample code for use within a Console Application. What I would like to do is use this code within a Windows Form. That...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.