none
Why WebClient.DownloadProgressChanged is fired only when download is completed? RRS feed

  • Question

  • I'm trying show the download progress step by step according with each byte is download, but my WebClient.DownloadProgressChanged is fired only when download is completed. How solve it?

        Private WithEvents myWebClient As New WebClient
        Dim installPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), Environment.MachineName)
    
        Private Sub Installer_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
            e.SuppressKeyPress = (e.Alt AndAlso e.KeyCode = Keys.F4)
        End Sub
    
        Private Sub Installer_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
    
            If Not Directory.Exists(installPath) Then
    
                Directory.CreateDirectory(installPath)
    
            End If
    
                myWebClient = New WebClient
                myWebClient.DownloadFileAsync(New Uri("https://site.com/myfile.zip"), Path.Combine(installPath, "myfile.zip"))
    
        End Sub
    
        Dim dTotalToDownloadMB As Decimal
        Dim dDownloadProgress As Decimal
        Dim dDownloadedMB As Decimal
    
        Public Sub myWebClient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles myWebClient.DownloadProgressChanged
            Try
                dDownloadProgress = CStr(e.BytesReceived / e.TotalBytesToReceive * 100)
                Progresso.Value = CInt(dDownloadProgress)
                dDownloadedMB = Format((e.BytesReceived / 1024) / 1024, "###,###,##0.00")
                dTotalToDownloadMB = Format((e.TotalBytesToReceive / 1024) / 1024, "###,###,##0.00")
                Me.Text = "Downloaded: " & dDownloadedMB & "MB out of " & dTotalToDownloadMB & "MB - Progress: " _
                        & Format(dDownloadProgress, "###.00") & "%"
            Catch ex As Exception
            End Try
        End Sub
    	
        Private Sub myWebClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles myWebClient.DownloadFileCompleted
            MsgBox("Download Completed", MsgBoxStyle.Information)
        End Sub

    Saturday, February 15, 2020 6:46 PM

All replies