views:

113

answers:

1
Imports System.Net
Public Class DownloadStuff
    Dim downloader As New WebClient()
    Private Sub Progress_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Progress.Validated
        AddHandler downloader.DownloadProgressChanged, AddressOf DownloadChangedHandler

        Dim uri As New Uri("http://www.example.com/example.txt")
        downloader.DownloadFileAsync(uri, "C:\example.txt")
    End Sub
    Private Sub DownloadChangedHandler(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
        Progress.Maximum = CInt(e.TotalBytesToReceive)
        Progress.Value = CInt(e.BytesReceived)
        Application.DoEvents()
    End Sub
End Class

This is my code, but the DownloadProgressChanged event is NEVER fired. (I am using an example URL here, but it is the same basic stuff)

What am I doing wrong? Progress is a ProgressBar.

This is in VB.net.

On MSDN, they mentioned something about overriding GetWebRequest, but I have no idea how to do that or what to do.

UPDATE: Still no progress, I simply cannot figure out how to make the handler fire.

A: 

Try this:

Sub Main()

    Dim client As WebClient = New WebClient()
    AddHandler client.DownloadProgressChanged, AddressOf DownloadProgressCallback
    client.DownloadFileAsync(New Uri("..."), "data.txt")

End Sub

Private Sub DownloadProgressCallback( _
    ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)

    Console.WriteLine(e.ProgressPercentage)

End Sub

Everytime I see AddHandler and Handles I feel dizzy.

Rubens Farias
Didn't work >.> http://www.developerfusion.com/tools/convert/csharp-to-vb/ <--- is what I tried
Cyclone
That is what I had essentially, note that the callback is never executed, ever.
Cyclone