Link to home
Start Free TrialLog in
Avatar of MyAsylum
MyAsylum

asked on

WebRequest/Response ... How can I detect download speed?

Hi there,
Here's a problem that I've found absolutely no help online or on MSDN with. Maybe someone can shed some light on this subject.
I have an application which downloads files from a web site. Everything works great. I would just like to implement a status label showing the download speed.

Here's the code for downloading the remote file and saving to a local file.

         HttpWebResponse resFile = (HttpWebResponse)reqFile.GetResponse();
                    // Once the WebResponse object has been retrieved,
                    // get the stream object associated with the response's data
                    remoteStream = resFile.GetResponseStream();

                    // Create the local file
                    localStream = File.Create(locFilName);

                    // Allocate a 15MB buffer
                    byte[] buffer = new byte[15360];
                    int bytesRead;

                    // Simple do/while loop to read from stream until
                    // no bytes are returned
                    do
                    {
                        // Read data (up to 15MB) from the stream
                        bytesRead = remoteStream.Read(buffer, 0, buffer.Length);

                        // Write the data to the local file
                        localStream.Write(buffer, 0, bytesRead);

                        // Increment total bytes processed
                        bytesProcessed += bytesRead;

                       // Update progressbar
                        this.Invoke(new updProgressDelegate(this.updProgress), 1);

                    } while (bytesRead > 0);
ASKER CERTIFIED SOLUTION
Avatar of iHadi
iHadi
Flag of Syrian Arab Republic image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry the second formula should be:
speed = Number of bytes now - Number of bytes in the previous second
Avatar of MyAsylum
MyAsylum

ASKER

Thanks for the quick reply. I'm trying to work out the second formula, but it's making my brain hurt ... ... quite badly. lol
I understand the concept of it, but I can't figure a way to calculate the number of bytes in the previous second.

Mainly because the timer_tick event is not within the download_file().

bytesProcessed is a global variable so I can grab that value with each tick, but how can I set anothing int var to calculate the number of bytes within the current second, ... then minus the number of bytes downloaded in the last second.

This is far definitely more challenging than I thought it would be ... unless I'm missing something.

Thanks for your help!!!!
-Danny
Ahhh ...
Nevermind ... I'm an idiot.

int bytesProcessedNew = 0;
int bytesProcessedLast = 0;

private void timSpeed_Tick(object sender, EventArgs e)
        {
            bytesProcessedLast = bytesProcessedNew;
            bytesProcessedNew = bytesProcessed;
           
            lblSpeed.Text = Convert.ToString((bytesProcessed - bytesProcessedLast) / 1024) + " KB/second";
        }

...
Works alright, but throughout the download, the speed fluxuates a great deal. It will report as "0" for a split,split second then go back to 145KB/sec etc ... I'll try and figure out why now.

Thanks so much for the help!!!