Clocking transfer rate in c#

N

None

I'm using the HttpWebRequest/Response methods and I'm trying to figure
out how to calculate the effective transfer rare the file is being
received at (like you see in IE when you download something). I have
already learned how to do a "progress bar" type solution, which lets
me calculate percentages. This is different however, because rate
implies time.

I've tried making a timer that goes off once a second and then
applying (bytes downloded now - bytes downloaded last interval), I get
inaccurate speed (584 KB/s when it's more like 230 KB/s)

then I tried recording the ticks between 2 successive read calls in a
loop, dividing that by the number of ticks in a second, getting the
inverse of that and multiplying by 4 (bc my buffer is 4k). Still no
good

If anyone has any solutions I'd love to hear them. Thanks.
 
G

Guest

I did something like this... It seems accurate

//SPEED STUF
counter++
int speed=lsize/counter
guid.labelSpeed.Text=speed.ToString()+" Kb/s"

this code is in my Timer1_Tick method, it is called every second. counter is type int and every second it is divided into the size of the file being downloaded to my hard drive. So if lsize = 126KB and counter =9; my download speed would be 14Kb/s (126/9=14). At least I think this is how it worked... I havent looked at this code in a longggg time

It sounds good to me. I just take the size of how much of the file has been placed on my drive, then divide the size by the amount of time it took to get that much. Yeah. this should work fine.
 
J

Jon Skeet [C# MVP]

None said:
I'm using the HttpWebRequest/Response methods and I'm trying to figure
out how to calculate the effective transfer rare the file is being
received at (like you see in IE when you download something). I have
already learned how to do a "progress bar" type solution, which lets
me calculate percentages. This is different however, because rate
implies time.

I've tried making a timer that goes off once a second and then
applying (bytes downloded now - bytes downloaded last interval), I get
inaccurate speed (584 KB/s when it's more like 230 KB/s)

then I tried recording the ticks between 2 successive read calls in a
loop, dividing that by the number of ticks in a second, getting the
inverse of that and multiplying by 4 (bc my buffer is 4k). Still no
good

Don't multiply by the buffer size - multiply by how many bytes you
actually received, which may not be the same as the buffer size.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top