[英]Downloading multiple files
First of all, sorry for my English :(. 首先,对不起我的英语:(。
Hi, I've this code to download multiple files at once, but it's very glitchy when I'm downloading small files. 嗨,我有这段代码可以一次下载多个文件,但是当我下载小文件时却很麻烦。 If I lets say download a file that's 80 KB, then the label where I display the progress is spacing out :(.
如果我说下载一个80 KB的文件,那么我显示进度的标签就是:(。
This is the code I have right now: 这是我现在拥有的代码:
bgwrkSplash.ReportProgress(44, 44444444444444);
ChangeText(lblStatus, "Downloading files to temp directory...", Color.Black);
DownloadFile();
resetEvent.WaitOne();
#region Download Handler
private void DownloadFile()
{
if (_downloadUrls.Any())
{
_intCurrentProgressValue = prgSplashStatus.Value;
_client.DownloadProgressChanged += client_DownloadProgressChanged;
_client.DownloadFileCompleted += client_DownloadFileCompleted;
var url = _downloadUrls.Dequeue();
var uri = new Uri(url);
_strFileName = Path.GetFileName(uri.LocalPath);
_client.DownloadFileAsync(new Uri(url), _strTempLocation + _strFileName);
}
}
private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
try
{
_byteCountFormatter.Add(new ByteCountFormatter {DataSize = e.BytesReceived, Time = DateTime.Now});
_byteCountFormatter = _byteCountFormatter.Skip(Math.Min(0, _byteCountFormatter.Count - 6)).ToList();
var speed = (_byteCountFormatter.Last().DataSize - _byteCountFormatter.First().DataSize)/
(_byteCountFormatter.Last().Time - _byteCountFormatter.First().Time).TotalSeconds;
var timeRemaining = TimeSpan.FromSeconds((e.TotalBytesToReceive - e.BytesReceived)/speed);
ChangeText(lblStatus, string.Format(
"Downloading {0} - {1} - {2} of {3} ({4})",
_strFileName,
ByteCountFormatter.FormatTime(timeRemaining),
ByteCountFormatter.FormatDataSize(e.BytesReceived),
ByteCountFormatter.FormatDataSize(e.TotalBytesToReceive),
ByteCountFormatter.FormatDataSpeed(speed)), Color.Black);
ChangeProgress(prgSplashStatus, e.ProgressPercentage);
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
try
{
try
{
if (e.Error != null)
{
throw e.Error;
}
if (e.Cancelled)
{
}
DownloadFile();
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
if (!_client.IsBusy)
{
ChangeProgress(prgSplashStatus, _intCurrentProgressValue);
resetEvent.Set();
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
#endregion
我删除了“剩余时间”部分,现在一切顺利,谢谢大家!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.