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
Solved

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

Posted on 2006-11-11
4
400 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 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