Solved

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

Posted on 2006-11-11
4
429 Views
Last Modified: 2010-04-16
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);
0
Comment
Question by:MyAsylum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
iHadi earned 125 total points
ID: 17924060
Hi MyAsylum

You can implenet a method to give you the average download speed by storing the time the download started and on a regular basis (maybe every second) calculate the average speed using the following formula:
Speed = NumberOfBytes / (Current Time - Begin Time)

If you want to display the real download speed , you'll have to count the number of bytes you download every second in a timer using the following formula:
speed = Number of bytes in the previous second - number of bytes now

If you need any help on the code for either methods just ask
0
 
LVL 13

Expert Comment

by:iHadi
ID: 17924065
Sorry the second formula should be:
speed = Number of bytes now - Number of bytes in the previous second
0
 

Author Comment

by:MyAsylum
ID: 17924392
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
0
 

Author Comment

by:MyAsylum
ID: 17924457
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!!!
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question