Solved

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

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 63
List<PaisEntity> - show some 1 24
Call Controller Action Method from ASPX 2 15
Problem with SelectList in .NET MVC application 1 17
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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