我正在制作一个下载大量文件的简单程序。现在我想向用户显示“xx剩余时间”(就像你用uTorrent看到的那样)。
到目前为止这是我得到的
正在下载文件
// Download the file
//Console.WriteLine("Downloading \"{0}\" from {1}...", node.Name, Config.FTP_HOST + "/" + node.DirectoryFileIsIn);
FtpWebRequest request = CreateRequest("ftp://" + Config.FTP_HOST + "/" + node.DirectoryFileIsIn + "/" + node.Name, WebRequestMethods.Ftp.DownloadFile.ToString());
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream filestream = new FileStream(new_path, FileMode.Create);
int chunkSize = 1024;
long total = node.FileSize;
int streamPosition = 0;
while (true)
{
byte[] buffer = new byte[Math.Min(chunkSize, node.FileSize - streamPosition)];
//byte[] buffer = new byte[chunkSize];
int readBytes = responseStream.Read(buffer, 0, buffer.Length);
if (readBytes == 0)
break;
currentFileSizeDownloaded += readBytes; // total bytes downloaded
bytesSnapshot += readBytes; // for tracking download speed
streamPosition += readBytes; // for tracking response stream position
filestream.Write(buffer, 0, readBytes);
}
filestream.Flush();
//Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
response.Dispose();
filestream.Dispose();
response.Close();
filestream.Close();
捕获下载的字节数(单独的线程)
// Calculate total download time
public static void CalculateDownloadTime()
{
bytesSnapshot = 0;
int waitTime = 10000;
Thread.Sleep(waitTime); // sleep 10 seconds
if (bytesSnapshot > 0)
{
double downloadSpeed = (bytesSnapshot / 10); // bytes per second
long remainingTime = (totalFileSizeToDownload - currentFileSizeDownloaded) / (long)downloadSpeed;
Console.WriteLine("Download speed {0}", Formatting.FormatBytes((long)downloadSpeed));
Console.WriteLine("Remaining time {0}", Formatting.FormatSeconds(remainingTime));
Console.WriteLine();
}
else
{
Console.WriteLine("byteSnapshot = 0"); // DEBUG
downloadTracker.Abort();
return;
}
CalculateDownloadTime();
}
输出
Download speed 106,73 KB
Remaining time 0:01:52
Download speed 9,02 KB
Remaining time 0:22:05
Download speed 8,94 KB
Remaining time 0:22:06
Download speed 7,68 KB
Remaining time 0:25:35
Download speed 7,68 KB
Remaining time 0:25:25
Download speed 8,12 KB
Remaining time 0:23:52
Download speed 8,39 KB
Remaining time 0:22:56
Download speed 169,65 KB
Remaining time 0:00:58
Download speed 591,03 KB
Remaining time 0:00:06
Download speed 393,86 KB
Remaining time 0:00:00
byteSnapshot = 0
你可以看到我做错了什么..下载速度对我来说看起来不太现实,时间已经过去了,但我无法弄清楚这里出了什么问题..
我认为我的问题在于下载速度的计算,所以如果下载速度正确,时间就会固定...
答案 0 :(得分:0)
我一直在测试,我认为我找到了解决方案。
我创建了2 BackgroundWorkers
下载过程
这里没有太大的改变..我添加了一个Stopwatch
来跟踪下载x字节数所花费的时间。 x个字节存储在currentFileSizeDownloaded
中,秒存储在secondsElapsed
。
// Download the file
//Console.WriteLine("Downloading \"{0}\" from {1}...", node.Name, Config.FTP_HOST + "/" + node.DirectoryFileIsIn);
FtpWebRequest request = CreateRequest("ftp://" + Config.FTP_HOST + "/" + node.DirectoryFileIsIn + "/" + node.Name, WebRequestMethods.Ftp.DownloadFile.ToString());
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream filestream = new FileStream(new_path, FileMode.Create);
// Start stopwatch for calculating download speed
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// Read/write downloaded bytes
int chunkSize = 1024;
long total = node.FileSize;
int streamPosition = 0;
while (true)
{
byte[] buffer = new byte[Math.Min(chunkSize, node.FileSize - streamPosition)];
//byte[] buffer = new byte[chunkSize];
int readBytes = responseStream.Read(buffer, 0, buffer.Length);
if (readBytes == 0)
break;
currentFileSizeDownloaded += readBytes; // total bytes downloaded
streamPosition += readBytes; // for tracking response stream position
filestream.Write(buffer, 0, readBytes);
}
// Stop the stopwatch
stopwatch.Stop();
secondsElapsed += stopwatch.Elapsed.TotalSeconds;
filestream.Flush();
//Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
response.Dispose();
filestream.Dispose();
response.Close();
filestream.Close();
}
计算下载速度
这是我计算下载速度的地方,再没有什么大的变化..我正在缓存secondsElapsed
,currentFileSizeDownloaded
来自下载过程并使用这些精确值进行我的DownloadSpeed计算(而不是使用'10我Thread.Sleep()
的秒数。
while (true)
{
cached_secondsElapsed = secondsElapsed;
cached_currentFileSizeDownloaded = currentFileSizeDownloaded;
Thread.Sleep(10000); // Sleep 10 seconds
long bytesDownloaded = currentFileSizeDownloaded - cached_currentFileSizeDownloaded;
double secondsItTook = secondsElapsed - cached_secondsElapsed;
if (bytesDownloaded > 0)
{
double downloadSpeed = ((double)bytesDownloaded / secondsItTook); // bytes per second
long remainingTime = (totalFileSizeToDownload - currentFileSizeDownloaded) / (long)downloadSpeed;
Console.WriteLine("Download speed {0}", Formatting.FormatBytes((long)downloadSpeed));
Console.WriteLine("Remaining time {0}", Formatting.FormatSeconds(remainingTime));
Console.WriteLine();
}
else
{
Console.WriteLine("bytesDownloaded = 0"); // DEBUG
Application.Exit(); // Stop the app
}
}
<强>输出强>
Download speed 976,58 KB
Remaining time 0:00:12
Download speed 438,6 KB
Remaining time 0:00:27
Download speed 483,37 KB
Remaining time 0:00:24
Download speed 325,42 KB
Remaining time 0:00:36
Download speed 331,42 KB
Remaining time 0:00:35
Download speed 363,87 KB
Remaining time 0:00:31
Download speed 441,13 KB
Remaining time 0:00:26
Download speed 798,81 KB
Remaining time 0:00:10
Download speed 1,04 MB
Remaining time 0:00:00
bytesDownloaded = 0
这不是那么准确,但它比以前更好...如果有人对我的提示更准确,请发布它们作为答案/评论。