-
Feb 26th, 2015, 05:37 AM
#1
Thread Starter
Fanatic Member
Dual progress bars: Current file, total files
The theory behind this seems straight forward but i am obviously missing the point. For example if we had 5 online files that need to be downloaded. We loop each link to obtain the total of files size. The webclient class provides an easy interface to download single files with a progresschanged method.
The issue is when i obtain the total of 5 files to be downloaded and attempt to have the progress bar work as a total % left nothing is returned. The progress bar does not change.
Taking the n number of fails to return file size
Code:
Private Function FileSize(url As String) As Integer
Return HttpWebRequest.Create(url).GetResponse().Headers.Get("Content-Length")
End Function
Code:
Private Sub mWebClient_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles mWebClient.DownloadProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Static n As Integer = 0
For Each i In Me.mUrls
n += FileSize(i)
Next
Me.ProgressBar1.Maximum = n
' start downloading files.....
End Sub
-
Feb 26th, 2015, 07:58 AM
#2
Lively Member
Re: Dual progress bars: Current file, total files
can you try this:
vb.net Code:
Me.ProgressBar1.Value += e.ProgressPercentage
-
Feb 26th, 2015, 08:03 AM
#3
Re: Dual progress bars: Current file, total files
Originally Posted by ShadowTzu
can you try this:
vb.net Code:
Me.ProgressBar1.Value += e.ProgressPercentage
E.ProgressPercentage reports the progress percentage. No need for =+
-
Feb 26th, 2015, 08:08 AM
#4
Lively Member
Re: Dual progress bars: Current file, total files
Originally Posted by ident
E.ProgressPercentage reports the progress percentage. No need for =+
Please read again:
The webclient class provides an easy interface to download single files with a progresschanged method.
-
Feb 26th, 2015, 08:26 AM
#5
Re: Dual progress bars: Current file, total files
You have set the maximum of the progress bar to the size of the files, which is probably a very large number. The 'progress percentage' probably goes from 0 to 100. You won't see 100 out of, say 1 million on the progress bar.
You need to keep track of when each file completes. So, if you have a file complete, that would be 100. Two files, 200, and so on. So, keep a running track of the completed files, and set the progress bar maximum to 100 * Number of files, and the progress bar to (100 * Completed files + current progress). That would be one way.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Feb 26th, 2015, 08:51 AM
#6
Re: Dual progress bars: Current file, total files
Originally Posted by kayleigh
The issue is when i obtain the total of 5 files to be downloaded and attempt to have the progress bar work as a total % left nothing is returned. The progress bar does not change.
Does your program actually just hang while trying to fetch the file sizes?
This line looked a bit dubious to me:
Code:
Return HttpWebRequest.Create(url).GetResponse().Headers.Get("Content-Length")
so I tried it, and it times out for me after checking 2 files.
There's a limit on the number of HTTP connections to the same server of either 2 or 4 depending on the protocol, and that line opens a (response) stream to the server but never closes it.
It may not be affecting you if you are downloading from different servers, or if you've altered the limit yourself. However, you should be closing the streams in any case.
-
Feb 26th, 2015, 09:01 AM
#7
Lively Member
Re: Dual progress bars: Current file, total files
I agree with Inferrd, here is a solution:
vb.net Code:
Private Function GetFileSize(Url As String) As Long Dim FileSize As Long = 0 Dim req As WebRequest = HttpWebRequest.Create(Url) req.Method = WebRequestMethods.Http.Head Dim resp As WebResponse = req.GetResponse() resp = req.GetResponse() FileSize = Integer.Parse(resp.Headers.Get("Content-Length")) resp.Close() resp.Dispose() resp = Nothing Return FileSize End Function
-
Feb 26th, 2015, 03:45 PM
#8
Re: Dual progress bars: Current file, total files
Originally Posted by ShadowTzu
I agree with Inferrd, here is a solution:
vb.net Code:
Private Function GetFileSize(Url As String) As Long Dim FileSize As Long = 0 Dim req As WebRequest = HttpWebRequest.Create(Url) req.Method = WebRequestMethods.Http.Head Dim resp As WebResponse = req.GetResponse() resp = req.GetResponse() FileSize = Integer.Parse(resp.Headers.Get("Content-Length")) resp.Close() resp.Dispose() resp = Nothing Return FileSize End Function
Thats identical to what is already posted.The OP wont have any issues likely since it's not likely from the same site.
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
|