简体   繁体   English

WebClient多文件下载器错误

[英]WebClient multi file Downloader error

im using the following code to download 50+ files from my webserver 即时通讯使用以下代码从我的网络服务器下载50多个文件

private void button5_Click(object sender, EventArgs e)
{

WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

//downloads
client.DownloadFileAsync(new Uri("http://www.site.com/file/loc.file"), @"c:\app\loc ");
client.DownloadFileAsync(new Uri("http://www.site.com/file/loc.file"), @"c:\app\loc ");
client.DownloadFileAsync(new Uri("http://www.site.com/file/loc.file"), @"c:\app\loc ");
 }

           void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        double bytesIn = double.Parse(e.BytesReceived.ToString());
        double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
        double percentage = bytesIn / totalBytes * 100;
        progressBar.Value = int.Parse(Math.Truncate(percentage).ToString());
    }

    private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        MessageBox.Show("Game Update Finished!");
    }

im wanting to download 1 file at a time with a continuing progress bar iv got most of the coding done but when i hit the "Download" button i get the following error 即时通讯想要一次下载一个连续进度条的文件iv完成了大部分编码,但是当我按下“下载”按钮时,出现以下错误

WebClient does not support concurrent I/O operations. WebClient不支持并发I / O操作。

what do i need to do? 我需要做什么?

Feel the difference: 感到不同:

  • It CAN download multiple files in parallel manner (by one stream per one file). 它可以并行方式下载多个文件(每个文件一个流)。
  • But it CAN'T download one file using multiple streams. 但是它不能使用多个流下载一个文件。

Here is example (MainWindow contain one button 'Start' and five progress bars): 这是示例(MainWindow包含一个按钮“开始”和五个进度条):

public partial class MainWindow : Window
{
    private WebClient _webClient;
    private ProgressBar[] _progressBars;
    private int _index = 0;

    public MainWindow()
    {
        InitializeComponent();
        _progressBars = new [] {progressBar1, progressBar2, progressBar3, progressBar4, progressBar5};
        ServicePointManager.DefaultConnectionLimit = 5;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Interlocked.Increment(ref _index);
        if (_index > _progressBars.Length)
            return;

        _webClient = new WebClient();
        _webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
        _webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;

        _webClient.DownloadFileAsync(new Uri("http://download.thinkbroadband.com/5MB.zip"),
                                     System.IO.Path.GetTempFileName(),
                                     _index);
    }

    private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs args)
    {
        var index = (int) args.UserState;
        _progressBars[index-1].Value = args.ProgressPercentage;
    }

    private void WebClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs args)
    {
        var index = (int)args.UserState;

        MessageBox.Show(args.Error == null
                            ? string.Format("Download #{0} completed!", index)
                            : string.Format("Download #{0} error!\n\n{1}", index, args.Error));
    }
}

you are running multiple downloads in parallel with the same WebClient instance - the error tells you that this is NOT supported - you either: 您正在与同一个WebClient实例并行运行多个下载-错误告诉您不支持此下载-您要么:

  • use multiple instances of WebClient (one per parallel download) OR 使用多个WebClient实例(每个并行下载一个)或
  • download one file after the other 依次下载一个文件

Relevant information: 相关信息:

WebClient does not support concurrent I/O operations (multiple downloads) per instance, so you need to create a separate WebClient instance for each download. WebClient不支持每个实例的并发I / O操作(多次下载),因此您需要为每次下载创建一个单独的WebClient实例。 You can still perform each download asynchronously and use the same event handlers. 您仍然可以异步执行每个下载,并使用相同的事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM