I am creating a program to download files from download websites. I've created a background worker to handle download large files as it usually freezes the UI when downloading large files.
I've managed to make it work but the problem I am facing now is that I am not able to use my AddHandler to show the changed progress, so I tried to use an invoke method for the progress changed values.
This is the code I tried for the invoke method:
Dim ProgressChanged As New ProgressChange(AddressOf bw_ProgressChanged)
Me.Invoke(ProgressChanged, Nothing, EventArgs.Empty)
This is my ProgressChanged handler.
Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString())
Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString())
Dim percentage As Double = bytesIn / totalBytes * 100
ProgressBarCurrent.Value = Int32.Parse(Math.Truncate(percentage).ToString())
Dim BytesDownloaded As String = (e.BytesReceived / (DirectCast(e.UserState, Stopwatch).ElapsedMilliseconds / 1000.0#)).ToString("#")
If BytesDownloaded < 1024 Then
Dim Bs As String = Convert.ToInt32(BytesDownloaded)
Label4.Text = (Bs & " B/s")
ElseIf BytesDownloaded < 1048576 Then
Dim KBs As String = Math.Round(BytesDownloaded / 1024, 2)
Label4.Text = (KBs & " KB/s")
ElseIf BytesDownloaded < 1073741824 Then
Dim MBs As String = Math.Round(BytesDownloaded / 1048576, 2)
Label4.Text = (MBs & " MB/s")
ElseIf BytesDownloaded < 1099511627776 Then
Dim GBs As String = Math.Round(BytesDownloaded / 1073741824, 2)
Label4.Text = (GBs & " GB/s")
Else
Label4.Text = ("Estimating...")
End If
End Sub
It's got some more code but I don't think it's necessary to show.
And this is my delegate sub.
Delegate Sub ProgressChange(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
I've also tried a few different things with addhandler method.
AddHandler wc.DownloadProgressChanged, AddressOf bw_ProgressChanged
Before when I used this code I was getting an error but now when I use it, there is no error but the code doesn't actually do anything, like it's not even fired, so I figured add handlers wouldn't work.
I wasn't sure if it was possible to use Invoke method for DownloadProgressChanged, but I believe it should be and I am not sure what arguments, I would use. I have tried different arguments that I thought would work but they didn't.