1

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.

avgcoder
  • 372
  • 1
  • 9
  • 27

1 Answers1

0

You need to call [YourBackgroundWorkerObject].ReportProgress from inside DoWork. This triggers the ProgressChanged event.
Your ProgressChanged-procedure must then invoke the method that does the UI changes.
(BTW, you can as well skip that Progress-Reporting-Reroute of the BGW. Invoke your own UI-changing method directly from DoWork.)

KekuSemau
  • 6,830
  • 4
  • 24
  • 34
  • Thanks for your reply. I just wanted to ask if I will still be able to use DownloadProgressChangedEventArgs? I'm asking this because at the moment I am using BytesReceived (function of DownloadProgressChangedEventArgs) to calculate the download speed and size. Is there another method I could use as an alternative, if I can't continue to use this? – avgcoder Aug 18 '13 at 19:42
  • I'm using AddHandler wc.DownloadProgressChanged, AddressOf bw_ProgressChanged but like I said before it doesn't actually do anything. – avgcoder Aug 18 '13 at 19:55
  • You can pass any arguments to your own method, maybe see one example [here](http://stackoverflow.com/questions/12533412/multithreading-for-a-progressbar-and-code-locations-vb-net) (in the answer). And as I said, ProgressChanged is not called automatically, you'd have to call via ReportProgress in your `DoEvents`-method (which you must write if you haven't yet). – KekuSemau Aug 18 '13 at 20:58
  • No, `DoWork`, not `DoEvents` (can't edit that any more...) And: be sure you note (in the linked sample code) the parts about `AddHandler bgw.DoWork` and `RunWorkerAsnyc`. If that's missing in your code, you need to start there. – KekuSemau Aug 18 '13 at 21:08
  • I've got both of those down. Here is my current code, if you want to have a look. http://pastebin.com/kYP3tky2 It is a bit messy since I have been trying a load of different things. – avgcoder Aug 18 '13 at 21:24