This is the method that calculate the speed of download:
private void CalculateDownload(int ln, BackgroundWorker backw)
{
string speed;
DateTime DownloadStart = DateTime.Now;
int dnsize = ln;
if (dnsize > 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);
}
}
And this is the method i'm using to download a file from ftp server. Each time a single file.
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;
}
The first file download in the CalculateDownload method i'm getting the file size:
The variable ln is for example 66349
Then the variable speed contain: Download speed: 21367 bps In the next file:
ln = 59892
Then speed is: Download speed: 25100 bps
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
In form1 i'm sending the file to download like this:
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 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);
}
}
And the progress changed event in form1:
private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.toolStripStatusLabel1.Text = e.UserState.ToString();
}
Why when i'm using breakpoint it's showing every file the speeed the time was downloaded but without breakpoint it's showing infinity ?