Results 1 to 8 of 8

Thread: Dual progress bars: Current file, total files

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    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

  2. #2
    Lively Member ShadowTzu's Avatar
    Join Date
    Oct 2014
    Location
    France
    Posts
    68

    Re: Dual progress bars: Current file, total files

    can you try this:
    vb.net Code:
    1. Me.ProgressBar1.Value += e.ProgressPercentage

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Dual progress bars: Current file, total files

    Quote Originally Posted by ShadowTzu View Post
    can you try this:
    vb.net Code:
    1. Me.ProgressBar1.Value += e.ProgressPercentage
    E.ProgressPercentage reports the progress percentage. No need for =+

  4. #4
    Lively Member ShadowTzu's Avatar
    Join Date
    Oct 2014
    Location
    France
    Posts
    68

    Re: Dual progress bars: Current file, total files

    Quote Originally Posted by ident View Post
    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.

  5. #5
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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."

  6. #6
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Dual progress bars: Current file, total files

    Quote Originally Posted by kayleigh View Post
    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.

  7. #7
    Lively Member ShadowTzu's Avatar
    Join Date
    Oct 2014
    Location
    France
    Posts
    68

    Re: Dual progress bars: Current file, total files

    I agree with Inferrd, here is a solution:

    vb.net Code:
    1. Private Function GetFileSize(Url As String) As Long
    2.         Dim FileSize As Long = 0
    3.         Dim req As WebRequest = HttpWebRequest.Create(Url)
    4.         req.Method = WebRequestMethods.Http.Head
    5.         Dim resp As WebResponse = req.GetResponse()
    6.  
    7.         resp = req.GetResponse()
    8.         FileSize = Integer.Parse(resp.Headers.Get("Content-Length"))
    9.         resp.Close()
    10.         resp.Dispose()
    11.         resp = Nothing
    12.  
    13.         Return FileSize
    14.     End Function

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Dual progress bars: Current file, total files

    Quote Originally Posted by ShadowTzu View Post
    I agree with Inferrd, here is a solution:

    vb.net Code:
    1. Private Function GetFileSize(Url As String) As Long
    2.         Dim FileSize As Long = 0
    3.         Dim req As WebRequest = HttpWebRequest.Create(Url)
    4.         req.Method = WebRequestMethods.Http.Head
    5.         Dim resp As WebResponse = req.GetResponse()
    6.  
    7.         resp = req.GetResponse()
    8.         FileSize = Integer.Parse(resp.Headers.Get("Content-Length"))
    9.         resp.Close()
    10.         resp.Dispose()
    11.         resp = Nothing
    12.  
    13.         Return FileSize
    14.     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
  •  



Click Here to Expand Forum to Full Width