none
Progress bar when downloading files is not updating until after the file has downloaded or not at all RRS feed

  • Question

  • Files are now downloading one at a time, but the progress bar isn't updating as needed. Small files it updates after file has downloaded.

    Large files it doesn't update at all.

    Here is my code:

    Imports System.Net
    Imports System.ComponentModel
    
    Public Class Form1
    
        Dim WithEvents WClient As New WebClient
    
        Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim ftpsite As String = "ftp://mysite.com/"
    
            '*** For Third Party Files ***
            Dim DB_3rd_FTPFiles() As String = {"File1.zip",
                                               "File2.exe",
                                               File3.zip",
                                               File4.zip"}
    
    
    
            For Each ThirdFTP As String In DB_3rd_FTPFiles
    
                If ThirdFTP = "File2.zip" Then
                    WClient.Credentials = New NetworkCredential("user", "Password")
                    Dim Downloadpath2 As New Uri(ftpsite & "Folder2/" & ThirdFTP)
                    AddHandler _WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
                    AddHandler _WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
                    WClient.DownloadFile(Downloadpath2, "C:\FakeEDrive\Install\3rdParty\" & ThirdFTP)
                    
                    WClient.Dispose() 
                    
               else
                    WClient.Credentials = New NetworkCredential("user", "password")
                    Dim Downloadpath As New Uri(ftpsite & ThirdFTP)
                    AddHandler WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
                    AddHandler WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
                    WClient.DownloadFile(Downloadpath, "C:\FakeEDrive\Install\3rdParty\" & ThirdFTP)
                
                    WClient.Dispose()
    
                End If
    
                
            Next
    
        End Sub
    
    
        Public Sub WC_DownloadComplete(sender As Object, e As AsyncCompletedEventArgs)
    
            Using webClient As New Net.WebClient
                RemoveHandler webClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
            End Using
    
        End Sub
    
        Private Sub WClient_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WClient.DownloadProgressChanged
    
            ProgressBar1.Value = e.ProgressPercentage
    
        End Sub
    
    End Class

    Tuesday, April 16, 2019 4:36 PM

Answers

  • Hi

    If you check the documentation, only the Async version of the method supports true WebClient ProgressReporting.

    At a guess, your options would be to change back to use DownloadAsync method where you can get full Progress data like THIS

    If you want to only have the Progress as the number of files processed then you would need to use the file count/total files * 100 as the ProgressPercentage.

    I might be wrong, but I think you are mixing up WebClient Report Progress with a BackGroundWorker Report Progress?


    Regards Les, Livingston, Scotland

    • Marked as answer by Broggy69 Wednesday, April 17, 2019 8:56 PM
    Tuesday, April 16, 2019 5:20 PM
  • Hi

    There is always a trade-off to be made. In your case, trading visual finer grained progress versus speed - speed usually wins :)

    You could query for each file length and use that for Progress.


    Regards Les, Livingston, Scotland

    • Marked as answer by Broggy69 Wednesday, April 17, 2019 8:56 PM
    Wednesday, April 17, 2019 3:44 PM
  • Hi

    If you check the documentation, only the Async version of the method supports true WebClient ProgressReporting.

    At a guess, your options would be to change back to use DownloadAsync method where you can get full Progress data like THIS

    If you want to only have the Progress as the number of files processed then you would need to use the file count/total files * 100 as the ProgressPercentage.

    I might be wrong, but I think you are mixing up WebClient Report Progress with a BackGroundWorker Report Progress?


    Regards Les, Livingston, Scotland

    I did try the total file count method and while that does work, I was hoping to have the download progress of the actual file themselves. The problem with DownloadAsync is it downloads every file at the same time taking too much internet resources at one time. Our networks will then throttle the download speeds and the large files take hours.

    Downloading one file at a time takes 15 minutes to download all files. But for now I think I will stick with progress bar of each file.

    • Marked as answer by Broggy69 Wednesday, April 17, 2019 8:56 PM
    Wednesday, April 17, 2019 3:20 PM

All replies

  • Hi

    If you check the documentation, only the Async version of the method supports true WebClient ProgressReporting.

    At a guess, your options would be to change back to use DownloadAsync method where you can get full Progress data like THIS

    If you want to only have the Progress as the number of files processed then you would need to use the file count/total files * 100 as the ProgressPercentage.

    I might be wrong, but I think you are mixing up WebClient Report Progress with a BackGroundWorker Report Progress?


    Regards Les, Livingston, Scotland

    • Marked as answer by Broggy69 Wednesday, April 17, 2019 8:56 PM
    Tuesday, April 16, 2019 5:20 PM
  • Hi

    If you check the documentation, only the Async version of the method supports true WebClient ProgressReporting.

    At a guess, your options would be to change back to use DownloadAsync method where you can get full Progress data like THIS

    If you want to only have the Progress as the number of files processed then you would need to use the file count/total files * 100 as the ProgressPercentage.

    I might be wrong, but I think you are mixing up WebClient Report Progress with a BackGroundWorker Report Progress?


    Regards Les, Livingston, Scotland

    I did try the total file count method and while that does work, I was hoping to have the download progress of the actual file themselves. The problem with DownloadAsync is it downloads every file at the same time taking too much internet resources at one time. Our networks will then throttle the download speeds and the large files take hours.

    Downloading one file at a time takes 15 minutes to download all files. But for now I think I will stick with progress bar of each file.

    • Marked as answer by Broggy69 Wednesday, April 17, 2019 8:56 PM
    Wednesday, April 17, 2019 3:20 PM
  • Hi

    There is always a trade-off to be made. In your case, trading visual finer grained progress versus speed - speed usually wins :)

    You could query for each file length and use that for Progress.


    Regards Les, Livingston, Scotland

    • Marked as answer by Broggy69 Wednesday, April 17, 2019 8:56 PM
    Wednesday, April 17, 2019 3:44 PM
  • Hi

    There is always a trade-off to be made. In your case, trading visual finer grained progress versus speed - speed usually wins :)

    You could query for each file length and use that for Progress.


    Regards Les, Livingston, Scotland

    How do I do that? I have seen samples, but they don't seem to work for an FTP site. Usually I will get an error: AN error has occured during download: Value if -1 is not valid for Maximum, Maximum must be greater than or equal to 0

    However when using an HTTP site the code works. I can share the code, but it doesn't make sense to me.

    Wednesday, April 17, 2019 4:05 PM
  • Hi

    Here is a completely untested bit of code.

    Code found HERE

    ' COMPLETELY UNTESTED
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim Sz As Long = 0
        Dim WithEvents WC As New Net.WebClient
        Dim MyURL As String = "MyFilePath"
        Private Sub Button1_Click(sender As Object, e As EventArgs)
            Sz = FindSize(MyURL)
    
            ' if successfull, sz now holds
            ' the size of file in MyURL
    
        End Sub
        Public Function FindSize(url As String) As Long
            Using WC
                Using s As IO.Stream = WC.OpenRead(url)
                    Return Long.Parse(WC.ResponseHeaders("Content-Length").ToString())
                End Using
            End Using
        End Function
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, April 17, 2019 5:51 PM
  • Hi

    Here is a completely untested bit of code.

    Code found HERE

    ' COMPLETELY UNTESTED
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim Sz As Long = 0
        Dim WithEvents WC As New Net.WebClient
        Dim MyURL As String = "MyFilePath"
        Private Sub Button1_Click(sender As Object, e As EventArgs)
            Sz = FindSize(MyURL)
    
            ' if successfull, sz now holds
            ' the size of file in MyURL
    
        End Sub
        Public Function FindSize(url As String) As Long
            Using WC
                Using s As IO.Stream = WC.OpenRead(url)
                    Return Long.Parse(WC.ResponseHeaders("Content-Length").ToString())
                End Using
            End Using
        End Function
    End Class


    Regards Les, Livingston, Scotland

    I am going to mark as done. I am not going to worry about it for now. 

    Thank you for all your help

    Wednesday, April 17, 2019 8:55 PM