Problems with BackgroundWorker1.ReportProgress

Trying to show datastream progress bar during download

Page 1 of 1

2 Replies - 5205 Views - Last Post: 03 February 2009 - 10:56 AM Rate Topic: -----

#1 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Problems with BackgroundWorker1.ReportProgress

Posted 02 February 2009 - 04:41 PM

Good times.. I have finally figured out how to download a file with background worker and downloader.

Now the form does not freeze..

I have only one problem left now... How do I get the progress bar to increment in tandem with the data flow?

I have listed my full code below. Towards the bottom where the DoWork() sub resides, I am just a few lines short of this year long goal.



Imports System.IO
Imports System.Net
Imports System.Text

Public Class Form1
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Public dataline As String										   'Reads data line by line from reader stream
	Public linecount As Integer										 'Counter will be used to keep track of how many lines of data
	Public datajoined As String '= dataline.Insert(0, dataline.Length)  'create string to hold datastream object
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		Me.Show()
		ProgressBar1.Value = 0			  'Initialize progress bar to start at 0
		BackgroundWorker1.RunWorkerAsync()  'Start back ground thread
		'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	End Sub

#Region " Primary thread methods "
	Private Sub BackgroundWorker1_ProgressChanged( _
  ByVal sender As Object, ByVal e As _
  System.ComponentModel.ProgressChangedEventArgs) _
  Handles BackgroundWorker1.ProgressChanged
		'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		ProgressBar1.Value = e.ProgressPercentage	 'Supposed to report download progress from Background thread
		''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	End Sub
	Private Sub BackgroundWorker1_RunWorkerCompleted( _
	 ByVal sender As Object, ByVal e As _
	 System.ComponentModel.RunWorkerCompletedEventArgs) _
	 Handles BackgroundWorker1.RunWorkerCompleted
		''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		ListBox1.Items.Add(datajoined)				 'Show beginning of long string in list box
		txtLines.Text = (linecount)					'Show # of lines read from webpage
		txtChars.Text = datajoined.Length			  'Show # of chars read from webpage
		''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	End Sub
	Private Sub btnCancel_Click(ByVal sender As System.Object, _
		ByVal e As System.EventArgs) Handles btnCancel.Click
		'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		BackgroundWorker1.CancelAsync()				 'Cancel button will stop background thread 
		'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	End Sub
#End Region
	''' <summary>
	''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	''' 
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>

#Region " Background thread methods "

	Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
		  ByVal e As System.ComponentModel.DoWorkEventArgs) _
		  Handles BackgroundWorker1.DoWork
		''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		'''''''''''''This sub downloads page data from the internet and then stores it in one really long string''''''''''''''''''''''''
		''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		Dim uri As New Uri("http://www.met.utah.edu/cgi-bin/droman/raws_ca_monitor.cgi?state=OR&rawsflag=3") 'URL to download from Internet
		Dim request As WebRequest = WebRequest.Create(uri)			 'Request website
		Dim response As WebResponse = request.GetResponse()			'Get response
		Dim stream As Stream = response.GetResponseStream()			'Get stream of data from site
		Dim reader As New StreamReader(stream)						 'Open Stream for reading
		'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		''''''''''''This loop reads page data line by line and then concatenates each line'''''''''''''

		While reader.EndOfStream <> True			'While we are not at the end of the streamreader
			dataline = reader.ReadLine			  'Read data line by line
			linecount += 1						  'Increment line counter to get total # of lines
			datajoined = datajoined + dataline	  'concatenate all datalines into one laerge string	

			'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
			''''''Heres the problem.. How do I report the progress of a datastream????????????''''''''''
			''''''I see how the prime numbers multi-thread example is coded'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
			''''''But I have not been able get the progress bar to report''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
			'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
			' If linecount > 900 Then
			'Me.BackgroundWorker1.ReportProgress(CInt(datajoined))
			'End If
			'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		End While
		'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		reader.Close() 'downloading now finished so close the page reader 
		stream.Close() 'close sream now that all data has been copied to memory
		'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		If Me.BackgroundWorker1.CancellationPending Then
			Exit Sub 'This will cancel the background thread
		End If
		''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	End Sub
#End Region

End Class





'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''Heres the problem.. How do I report the progress of a datastream????????????''''''''''
''''''I see how the prime numbers multi-thread example is coded'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''But I have not been able get the progress bar to report''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' If linecount > 900 Then
'Me.BackgroundWorker1.ReportProgress(CInt(datajoined))
'End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

This post has been edited by Schmit38: 02 February 2009 - 04:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problems with BackgroundWorker1.ReportProgress

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,462
  • Joined: 29-May 08

Re: Problems with BackgroundWorker1.ReportProgress

Posted 02 February 2009 - 05:22 PM

For as I can tell you're give it a percentage of work completed.
Percentage=Cint((Current Done / Total To Do)*100)

I tend to write my on threads and use delegate to interface with progress bar, it more complex but it allow you do a lot more.
Was This Post Helpful? 0
  • +
  • -

#3 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Re: Problems with BackgroundWorker1.ReportProgress

Posted 03 February 2009 - 10:56 AM

View PostAdamSpeight2008, on 2 Feb, 2009 - 04:22 PM, said:

For as I can tell you're give it a percentage of work completed.
Percentage=Cint((Current Done / Total To Do)*100)

I tend to write my on threads and use delegate to interface with progress bar, it more complex but it allow you do a lot more.


My problem is the backgroundworker example reports bar percentage based on a 1 to 100 loop

I am reading to the EOF on the stream which is variable in length. I dont find out how long the file is until after it loads.

Is there a way to get file header info ahead of time? (such as stream.length?)

There are 1815 lines and 85404 characters loaded into memory from this live weather page

But if one station drops off the list next hour then the line/char total will change.

I tried delegates last year and failed to bind progress bar to report progress.. Same fate now with backgroundworker.

WHY IS THIS SO DIFFICULT!!! :angry: Please help me!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1