I'm trying to get the current download speed of a WebClient downloading a file, however when I use a formula that I'm pretty sure should work out:
Stopwatch.Stop();
double msElapsed = Stopwatch.Elapsed.TotalMilliseconds;
int bytesDownloaded = (int)e.BytesReceived - lastBytesDownloaded;
double downloadSpeed = (double)bytesDownloaded / (msElapsed / 1000);
lastBytesDownloaded = (int)e.BytesReceived;
Stopwatch.Restart();
Where Stopwatch is a stopwatch that I've started just as I started the file download, lastBytesDownloaded is a class variable, and this is all inside the downloadProgressChanged event, however the download speed varies wildly off course from what it actually is.
For example if I was downloading a file at 500kb/s, it would rapidly jump from (for example) 10kb/s to 50mb/s completely randomly.
I can get an accurate average download time by making a couple edits to that:
double sElapsed = Stopwatch.Elapsed.TotalSeconds;
int bytesDownloaded = (int)e.BytesReceived;
double downloadSpeed = bytesDownloaded / sElapsed;
But that isn't what I want. How can I get a more stable reading for current download speed?