0

i want to implement download rate in my downloader .After googling found a rather complicated app Download manager - limit download speed.Then ,on another thread https://stackoverflow.com/questions/26073779/how-to-add-download-rate-and-limit-rate-in-downloader-in-c-sharp found a good suggestion:

Set up a timer which fires every second, and use a counter to record how many bytes have been downloaded, report the download rate as x Bytes/s in the timer Tick event, also reset the counter to zero

Just got on implementing this, using

bytesIn = int.Parse(e.BytesReceived.ToString());
totalBytes = int.Parse(e.TotalBytesToReceive.ToString());

I have bytesIn in my code showing bytesreceived yet but how to implement the suggestion quoted above using timers as if i use a tick() event and count it on every tick() event it won't show me my down-speed.

Suggestions please?

Community
  • 1
  • 1
  • Also keep track of the total download time so far... But that suggestion is just for displaying rate, not limiting it. Which are you trying to do? – Ben Voigt Oct 21 '14 at 13:37
  • BTW, TCP rate controls are easy on the sender, barely possible in the receiver network stack by using delayed or dropped ACK to create backpressure, and impossible for a receiver only using high level APIs. If you don't want TCP to determine the rate, you need rate control commands in the application layer, to get help from the sender. – Ben Voigt Oct 21 '14 at 13:40

1 Answers1

0

What you need to do is record bytesIn during the timer and store it for later. The next time the timer fires (one second later) you take the new bytesIn value then subtract the previous value. This will get you the number of bytes downloaded in the last second. You could show this number on screen as #### Bytes/s you could also divide that number by 1024 and display the result as #### Kbytes/s.

long lastDownload = 0;
long bytesIn = 0

//This is a event for a timer that fires once per second on the UI thread.
private void TimerTick(object sender, EventArgs e)
{
     //Figure out how many bytes where downloaded in the last second.
     var bytesDownloadedLastSecond = bytesIn - lastsDownload;

     //Copy the number over for the next firing of TimerTick.
     lastDownload = bytesIn;


     double scalingFactor;    
     string formatText;

     //Figure out if we should display in bytes, kilobytes or megabytes per second.
     if(bytesDownloadedLastSecond < 1024)
     {
          formatText = "{0:N0} B/sec";
          scalingFactor = 1;
     }
     else if(bytesDownloadedLastSecond < 1024 * 1024)
     {
          formatText = "{0:N2} Kb/sec";
          scalingFactor = 1024;
     }
     else
     {
          formatText = "{0:N2} Mb/sec";
          scalingFactor = 1024 * 1024;
     }

     //Display the speed to the user, scaled to the correct size.
     speedTextBox.Text = String.Format(formatText, bytesDownloadedLastSecond / scalingFactor );
}

private void DownloadProgress(object sender, EventArgs e)
{
    bytesIn = int.Parse(e.BytesReceived.ToString());
    totalBytes = int.Parse(e.TotalBytesToReceive.ToString());
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • 1
    ... except use actual time, not the requested timer interval. Even better would be times from actual receipt of data (and fall back to timer times if data stops coming). And some averaging. – Ben Voigt Oct 21 '14 at 13:49
  • @BenVoigt very true, this was just some very quick back of the napkin programming. To the OP, there is many things you can do to get a "better" speed indicator, do not take my answer as "the only way to do it." – Scott Chamberlain Oct 21 '14 at 13:51
  • ' speedTextBox '.I am unable to figure out even using google @ScottChamberlain –  Oct 22 '14 at 08:02
  • 1
    @DavProgMania is a placeholder name for a control on your form where you are printing the speed. It could easily be replaced with `Console.WriteLine(String.Format(formatText, bytesDownloadedLastSecond / scalingFactor ));` – Scott Chamberlain Oct 22 '14 at 13:28
  • @ScottChamberlain Can you take a gander at pls. http://stackoverflow.com/questions/26504979/how-to-get-the-auto-extension-of-entered-link-to-downloader –  Oct 23 '14 at 05:12