title: I'm trying to get ftp download file speed but sometimes i'm getting infinity why?
body: <p>This is the method that calculate the speed of download:</p>
  
  <pre><code>private void CalculateDownload(int ln, BackgroundWorker backw)
  {
      string speed;
      DateTime DownloadStart = DateTime.Now;
  
      int dnsize = ln;
  
      if (dnsize &gt; 0)
      {
          DateTime DownloadEnd = DateTime.Now;
          TimeSpan DownloadSub = DownloadEnd.Subtract(
              DownloadStart);
  
          speed = string.Format("Download speed: {0:F0} bps\n",
              (dnsize / DownloadSub.TotalMilliseconds) * 1000);
          backw.ReportProgress(0,speed);
      }
  }
  </code></pre>
  
  <p>And this is the method i'm using to download a file from ftp server. Each time a single file.</p>
  
  <pre><code>public int FtpDownload(object sender, string file, string filesdirectories, string fn)
  {
      string tmp = "";
  
      BackgroundWorker bw = sender as BackgroundWorker;
      string filenameonly = Path.GetFileName(file);
      string ftpdirectories = Path.Combine(ftpcontentdir, filesdirectories);
      string fileurl = "ftp://" + file;
      FtpWebRequest reqFTP;
      reqFTP = (FtpWebRequest)FtpWebRequest.Create(fileurl);
      reqFTP.Credentials = new NetworkCredential(UserName, Password);
      reqFTP.UseBinary = true;
      reqFTP.UsePassive = true;
      reqFTP.KeepAlive = true;
  
      reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
      try
      {
          FtpWebResponse response = (FtpWebResponse)reqFTP.
              GetResponse();
  
          Stream responseStream = response.GetResponseStream();
          StreamReader reader = new StreamReader(responseStream);
          tmp = reader.ReadToEnd();
          CalculateDownload(tmp.Length, bw);
          reader.Close();
          response.Close();
      }
      catch (WebException e)
      {
          Console.WriteLine(e.ToString());
      }
  
      return tmp.Length;
  }
  </code></pre>
  
  <p>The first file download in the CalculateDownload method i'm getting the file size:</p>
  
  <p>The variable ln is for example 66349</p>
  
  <p>Then the variable speed contain: Download speed: 21367 bps
  In the next file:</p>
  
  <p>ln = 59892</p>
  
  <p>Then speed is: Download speed: 25100 bps</p>
  
  <p>That is working fine when i'm using breakpoint but if i'm not using a breakpoint instead 25100 bps i will see infinity bps</p>
  
  <p>In form1 i'm sending the file to download like this:</p>
  
  <pre><code>private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
  {
      for (int i = 0; i &lt; numberOfFiles.Count; i++)
      {
          int fn = numberOfFiles[i].IndexOf(txtHost.Text, 0);
          string fn1 = numberOfFiles[i].Substring(txtHost.Text.Length + 1, numberOfFiles[i].Length - (txtHost.Text.Length + 1));
          string dirs = Path.GetDirectoryName(fn1);
          string filename = Path.GetFileName(fn1);
          ftpProgress1.FtpDownload(sender, numberOfFiles[i], dirs, filename);
      }
  }
  </code></pre>
  
  <p>And the progress changed event in form1:</p>
  
  <pre><code>private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
  {           
      this.toolStripStatusLabel1.Text = e.UserState.ToString();
  }
  </code></pre>
  
  <p>Why when i'm using breakpoint it's showing every file the speeed the time was downloaded but without breakpoint it's showing infinity ?</p>