Solved

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

Posted on 2006-11-11
4
367 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
Comment Utility
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
Comment Utility
Sorry the second formula should be:
speed = Number of bytes now - Number of bytes in the previous second
0
 

Author Comment

by:MyAsylum
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What is MicroStrategy.NET? 2 43
c# code 19 56
Image(7) 1 32
How to Get total minutes between two hours 4 26
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now