If your question is "how do I calculate the transfer rate", you can divide the total number of downloaded bytes by the total number of seconds that passed.
bytes
--------- = transfer rate
seconds
A good way to measure time in C# is the StopWatch class, and since K in computer science is 1024 (or 2^10) you can divide the number of bytes with 1024 (or shift it) and then divide it with the number of seconds it took to download that number of kilobytes.
If you are interested in an average transfer rate, you need to measure the downloaded bytes in intervals. You could do this with a two dimensional list, holding measure points and the downloaded bytes and time it took. For simplicity, break it of into a class that does the calculations
private readonly Stopwatch watch;
private readonly long[,] average;
public .ctor() {
// use 10 measure points, for a larger or smaller average, adjust the 10
average = new long[10, 2];
watch = Stopwatch.StartNew();
}
public long BytesTransferred {
set {
for (int i = average.GetLength(0) - 1; i > 0; --i) {
average[i, 0] = average[i - 1, 0];
average[i, 1] = average[i - 1, 1];
}
average[0, 0] = sent = value;
average[0, 1] = watch.ElapsedMilliseconds;
}
}
public long TransferRate {
get {
int l = average.GetLength(0) - 1;
double bytes = average[0, 0] - average[l, 0];
double seconds = (average[0, 1] - average[l, 1]) / 1000d;
return (long)(bytes / seconds);
}
}
In your download method, break of a new thread, create an instance of the class above, and call BytesTransferred = totalBytes;
in every interval. The TransferRate will be calculated every time you call TransferRate. Note that it is bytes/s, if you want another unit, divide by 1024 accordingly.