0

首先,对不起我的英语:(。

嗨,我有这段代码可以一次下载多个文件,但是当我下载小文件时,它非常有问题。如果我说下载一个 80 KB 的文件,那么我显示进度的标签就是间隔 :(。

这是我现在拥有的代码:

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

1 2

4

1 回答 1

0

我已经删除了剩余时间部分,现在一切顺利,谢谢大家!

于 2012-11-14T23:20:48.183 回答