Black Night
Black Night

Reputation: 1

WebClient multi file Downloader error

im using the following code to download 50+ files from my webserver

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

WebClient does not support concurrent I/O operations.

what do i need to do?

Upvotes: 0

Views: 3242

Answers (3)

white.zaz
white.zaz

Reputation: 1137

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):

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));
    }
}

Upvotes: 2

Yahia
Yahia

Reputation: 70369

you are running multiple downloads in parallel with the same WebClient instance - the error tells you that this is NOT supported - you either:

  • use multiple instances of WebClient (one per parallel download) OR
  • download one file after the other

Relevant information:

Upvotes: 1

Chris Hutchinson
Chris Hutchinson

Reputation: 9212

WebClient does not support concurrent I/O operations (multiple downloads) per instance, so you need to create a separate WebClient instance for each download. You can still perform each download asynchronously and use the same event handlers.

Upvotes: 0

Related Questions