Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient DownloadFileAsync - How can I display download speed to the user?

Tags:

c#

webclient

That's pretty much the whole question in the title. I have a WPF C# Windows application, I download files for the user and now want to display the speed.

like image 266
Drahcir Avatar asked Jul 17 '12 12:07

Drahcir


1 Answers

mWebClient.DownloadProgressChanged += (sender, e) => progressChanged(e.BytesReceived);
//...
DateTime lastUpdate;
long lastBytes = 0;

private void progressChanged(long bytes)
{
    if (lastBytes == 0)
    {
        lastUpdate = DateTime.Now;
        lastBytes = bytes;
        return;
    }

    var now = DateTime.Now;
    var timeSpan = now - lastUpdate;
    var bytesChange = bytes - lastBytes;
    var bytesPerSecond = bytesChange / timeSpan.Seconds;

    lastBytes = bytes;
    lastUpdate = now;
}

And do whatever you need with the bytesPerSecond variable.

like image 120
Tomas Grosup Avatar answered Oct 22 '22 23:10

Tomas Grosup