title: Accurate measurement of download speed of a webclient
body: <p>I am using C# and the WebClient class.</p>
  
  <p>This is the code I am using, inspired by another post here on SO. This code worked well for large files, it accurately displayed the download speed. However, there is now the limitation of downloading individual files, many of which are small, with small being .5-5 MB. This has caused the speed counter to skyrocket, often into the hundreds of thousands of KBps. I'm not sure what else to try to combat this. I added a second progress bar showing individual file downloads which helps improve the image a bit, but the download speed counter should really be fixed. Is there a different class to use that would solve this problem?</p>
  
  <p>The WebClient in this code is disposed of properly elsewhere.
         private class NetSpeedCounter
          {
              private double[] DataPoints;</p>
  
  <pre><code>        private DateTime LastUpdate;
          private int NumCounts = 0;
          private int PrevBytes = 0;
  
          public double Speed { get; private set; }
  
          public NetSpeedCounter(WebClient webClient, int maxPoints = 10)
          {
              DataPoints = new double[maxPoints];
  
              Array.Clear(DataPoints, 0, DataPoints.Length);
              webClient.DownloadProgressChanged += (sender, e) =&gt;
              {
                  var msElapsed = DateTime.Now - LastUpdate;
  
                  int curBytes = (int)(e.BytesReceived - PrevBytes);
                  PrevBytes = (int)e.BytesReceived;
  
                  double dataPoint = ((double)curBytes) / msElapsed.TotalSeconds;
                  DataPoints[NumCounts++ % maxPoints] = dataPoint;
  
                  Speed = DataPoints.Average();
              };
          }
  
          public void Reset()
          {
              PrevBytes = 0;
              LastUpdate = DateTime.Now;
          }
      }
  </code></pre>
  
  <p>I download the files with this code, which is started afterwards by a call to DownloadFileAsync. This code just downloads them in a chain, one after another, asynchronously.</p>
  
  <p>This is setting up for starting the download
          Queue recordQ = new Queue(files);</p>
  
  <pre><code>    progressBar.Value = 0;
      progressBar.Maximum = recordQ.Count;
  
      UpdateStatusText("Downloading " + recordQ.Count + " files");
  
      var record = recordQ.Dequeue();
      speedUpdater.Start();
      CheckAndCreate(record.AbsolutePath);
  </code></pre>
  
  <p>Adding the event handler</p>
  
  <pre><code>    wc.DownloadFileCompleted += (sender, e) =&gt;
      {
      var nr = recordQ.Dequeue();
      CheckAndCreate(nr.AbsolutePath);
      this.Invoke((MethodInvoker)delegate
      {
          UpdateStatusText("Downloading " + recordQ.Count + " files", lblStatusR.Text);
      });
      counter.Reset();
      // download the next one
      wc.DownloadFileAsync(nr.DownloadPath, nr.AbsolutePath);
   }
   counter.Start();
   wc.DownloadFileAsync(record.DownloadPath, record.AbsolutePath);
  </code></pre>
  
  <p>This last call is what starts everything off.</p>