C# 下载多个文件

C# 下载多个文件,c#,download,backgroundworker,C#,Download,Backgroundworker,首先,对不起我的英语:( 您好,我有一段代码可以一次下载多个文件,但是当我下载小文件时,它非常容易出错。如果让我说下载一个80 KB的文件,那么我显示进度的标签是间隔的:( 这是我现在掌握的代码: bgwrkSplash.ReportProgress(44, 44444444444444); ChangeText(lblStatus, "Downloading files to temp directory...", Color.Black); DownloadFile(); resetEven

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

您好,我有一段代码可以一次下载多个文件,但是当我下载小文件时,它非常容易出错。如果让我说下载一个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

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

你能分享或解释一下结果到底是什么,以及你对代码的期望是什么吗?我希望代码喜欢从队列中下载多个文件。它确实下载了这些文件,但看起来确实有问题:(.buggy,你的意思是文件顺序正在改变吗?不是真的,标签上显示的文本正在疯狂,它正在显示文本,但是文本被洗牌了等等,我真的不知道如何解释:(.你能分享一个屏幕截图或什么吗?