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
            End If
                myWebClient = New WebClient
                myWebClient.DownloadFileAsync(New Uri(""), Path.Combine(installPath, ""))
        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
                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