|
-
Nov 27th, 2012, 06:10 AM
#81
New Member
Re: Using the BackgroundWorker Component
Code:
BackgroundWorker1_DoWork
Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
Dim lines As String() = FileToArray(My.Computer.FileSystem.CurrentDirectory & "/filelist.txt")
Dim line As String
For Each line In lines
uriSource = New Uri("http://localhost/" & line)
uriPath = My.Computer.FileSystem.CurrentDirectory & line
???For i As Integer = 1 To 100
worker.ReportProgress(i, "Downloading : " & line)
??????Next i
downloading.DownloadFile(uriSource, uriPath)
Next line
----------------
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
Me.Label6.Text = TryCast(e.UserState, String)
End Sub
i Would like to know how to make progress bar work with my "for each" line reader for downloading. for example how should i get BytesReceived .
or like
Code:
ProgressBar1.Maximum = e.TotalBytesToReceive
ProgressBar1.Value = e.BytesReceived
thanks in advance!!
-
Nov 27th, 2012, 06:25 AM
#82
New Member
Re: Using the BackgroundWorker Component
sry for duplicate post website lagged
Last edited by Tompkins; Nov 27th, 2012 at 06:43 AM.
Reason: duplicate post :/
-
Nov 27th, 2012, 07:06 AM
#83
Re: Using the BackgroundWorker Component
 Originally Posted by Tompkins
Code:
BackgroundWorker1_DoWork
Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
Dim lines As String() = FileToArray(My.Computer.FileSystem.CurrentDirectory & "/filelist.txt")
Dim line As String
For Each line In lines
uriSource = New Uri("http://localhost/" & line)
uriPath = My.Computer.FileSystem.CurrentDirectory & line
???For i As Integer = 1 To 100
worker.ReportProgress(i, "Downloading : " & line)
??????Next i
downloading.DownloadFile(uriSource, uriPath)
Next line
----------------
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
Me.Label6.Text = TryCast(e.UserState, String)
End Sub
i Would like to know how to make progress bar work with my "for each" line reader for downloading. for example how should i get BytesReceived .
or like
Code:
ProgressBar1.Maximum = e.TotalBytesToReceive
ProgressBar1.Value = e.BytesReceived
thanks in advance!!
There are various options but the simplest is to just look at the name of the first parameter of the ReportProgress method: percentProgress. Instead of setting the Maximum of the ProgressBar to the total number of bytes and the Value to the actual number of bytes, simply leave the Maximum as the default 100 and calculate the percentage yourself and pass that one value, e.g. if you have 5,000,000 bytes to download and you've downloaded 2,000,000 then the percentage is 40% so that's what you pass to ReportProgress.
-
Nov 27th, 2012, 07:28 AM
#84
New Member
Re: Using the BackgroundWorker Component
 Originally Posted by jmcilhinney
There are various options but the simplest is to just look at the name of the first parameter of the ReportProgress method: percentProgress. Instead of setting the Maximum of the ProgressBar to the total number of bytes and the Value to the actual number of bytes, simply leave the Maximum as the default 100 and calculate the percentage yourself and pass that one value, e.g. if you have 5,000,000 bytes to download and you've downloaded 2,000,000 then the percentage is 40% so that's what you pass to ReportProgress.
How this should look like? at which point i should send that percentage with reportprogress in for each loop. can you show me some sample? Thanks for response!
-
Nov 27th, 2012, 07:36 AM
#85
Re: Using the BackgroundWorker Component
 Originally Posted by Tompkins
How this should look like? at which point i should send that percentage with reportprogress in for each loop. can you show me some sample? Thanks for response!
Anyone who has done primary school maths knows how to calculate a percentage so I'm not going to provide an example of that. As for where to call ReportProgress, just ask yourself where in your code you want to update the ProgressBar and that's where you call ReportProgress.
-
Nov 27th, 2012, 07:53 AM
#86
New Member
Re: Using the BackgroundWorker Component
 Originally Posted by jmcilhinney
Anyone who has done primary school maths knows how to calculate a percentage so I'm not going to provide an example of that. As for where to call ReportProgress, just ask yourself where in your code you want to update the ProgressBar and that's where you call ReportProgress.
I want to update ProgressBar while downloading each file. For example its downloading one file then the progressbar fillsup, and then when it starts to download other file progress bar starts from begining just for different file size. As i can understand you are telling me the example for overall files being downloaded. Sorry for my bad english .. And thanks for your patience and help!
Code:
Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
Dim lines As String() = FileToArray(My.Computer.FileSystem.CurrentDirectory & "/filelist.txt")
Dim line As String
Dim j As Integer
Dim percent As Short
For Each size In sizes
Dim i As Integer = Convert.ToInt32(size)
j = j + i
Next size
For Each line In lines
Console.WriteLine(line)
uriSource = New Uri("http://localhost/" & line)
uriPath = My.Computer.FileSystem.CurrentDirectory & line
worker.ReportProgress(percent, "Downloading : " & line)
downloading.DownloadFile(uriSource, uriPath)
Dim info As New FileInfo(My.Computer.FileSystem.CurrentDirectory & line)
Dim length As Long = info.Length
Dim g As Long
g = g + length
percent = (g * 100) / j
Next line
this is for overall. it works, but what about each file Thank You!
Last edited by Tompkins; Dec 3rd, 2012 at 08:22 AM.
Reason: added some information
-
Feb 26th, 2013, 01:52 AM
#87
New Member
Re: Using the BackgroundWorker Component
Hi John,
Thanks for useful explanation.
What about showing a message in another form, such as a none BorderStyleForm with a pic on it saying "please wait" or "loading" during the progress and closing the form after completion?
-
May 8th, 2014, 03:21 PM
#88
Re: Using the BackgroundWorker Component
With so many current BGW threads i think this should be on page One.
-
May 8th, 2014, 05:15 PM
#89
Re: Using the BackgroundWorker Component
Cross thread operation error...
Ident, you should know better than that... you know you can't access the UIThread from the BGW directly... it has to be invoked and passed back to the main UI processing thread... THEN you can get it to show up on the main form (Page1)...
-tg
-
May 9th, 2014, 05:55 AM
#90
Re: Using the BackgroundWorker Component
Took me a while to catch that one...has me looking on page 1 for ident's post rofl
-
May 9th, 2014, 08:29 AM
#91
Re: Using the BackgroundWorker Component
 Originally Posted by Niya
Took me a while to catch that one...has me looking on page 1 for ident's post rofl
90% of me says techgnome is making a joke "thread" related, the rest is left a little confused if not
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|