-2

I might be very stupid but how to solve the following? When I want to download many files I use a list of links and threaded WebClient.DownloadFileAsync. But I want my UI to be updated (ProgressBar) during the process so I used this answer to partially solve the problem.

But when I apply this part of code

void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        this.Dispatcher.BeginInvoke((Delegate MethodInvoker)
        {
            double bytesIn = double.Parse(e.BytesReceived.ToString());
            double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
            double percentage = bytesIn / totalBytes * 100;
            thebar.Value = int.Parse(Math.Truncate(percentage).ToString());
        });
    }

I get the " 'System.Delegate' is a 'type' but is used like a 'variable' " error.

Community
  • 1
  • 1
GeeMitchey
  • 134
  • 1
  • 3
  • 13

1 Answers1

1

You can call Dispatcher.BeginInvoke() to run a delegate on the WPF UI thread.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Okay then, this helped with the first problem. Many thanks, even though I couldn't give it a try because the second one still remains. – GeeMitchey Nov 30 '14 at 16:12
  • Use the `Action` delegate. – SLaks Nov 30 '14 at 16:14
  • You need to learn the basics of delegates & anonymous methods. http://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx – SLaks Nov 30 '14 at 16:25
  • Specifically, you need to cast the anonymous method to a compatible delegate type. – SLaks Nov 30 '14 at 16:25
  • Heck, still nothing. I understand what's on that MSDN page but don't catch it. I'm not a professional in all this (used to code in my free time), so I would be very pleased if you helped me getting over that so-called anonymous method. – GeeMitchey Nov 30 '14 at 16:44
  • You can create an anonymous method and convert it to the `Action` delegate type by writing `new Action(delegate { ... })` (a regular cast would also work) – SLaks Nov 30 '14 at 16:45