Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6242023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:52:31+00:00 2026-05-24T11:52:31+00:00

It has been surprisingly hard to find a code example of downloading multiple files

  • 0

It has been surprisingly hard to find a code example of downloading multiple files using the webclient class asynchronous method, but downloading one at a time.

How can I initiate a async download, but wait until the first is finished until the second, etc. Basically a que.

(note I do not want to use the sync method, because of the increased functionality of the async method.)

The below code starts all my downloads at once. (the progress bar is all over the place)

private void downloadFile(string url)
        {
            WebClient client = new WebClient();

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

            // Starts the download
            btnGetDownload.Text = "Downloading...";
            btnGetDownload.Enabled = false;
            progressBar1.Visible = true;
            lblFileName.Text = url;
            lblFileName.Visible = true;
            string FileName = url.Substring(url.LastIndexOf("/") + 1,
                            (url.Length - url.LastIndexOf("/") - 1));
             client.DownloadFileAsync(new Uri(url), "C:\\Test4\\" + FileName);

        }

        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {

        }

        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;
            progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:52:32+00:00Added an answer on May 24, 2026 at 11:52 am

    What I have done is populate a Queue containing all my urls, then I download each item in the queue. When there are no items left, I can then process all the items. I’ve mocked some code up below. Keep in mind, the code below is for downloading strings and not files. It shouldn’t be too difficult to modify the below code.

        private Queue<string> _items = new Queue<string>();
        private List<string> _results = new List<string>();
    
        private void PopulateItemsQueue()
        {
            _items.Enqueue("some_url_here");
            _items.Enqueue("perhaps_another_here");
            _items.Enqueue("and_a_third_item_as_well");
    
            DownloadItem();
        }
    
        private void DownloadItem()
        {
            if (_items.Any())
            {
                var nextItem = _items.Dequeue();
    
                var webClient = new WebClient();
                webClient.DownloadStringCompleted += OnGetDownloadedStringCompleted;
                webClient.DownloadStringAsync(new Uri(nextItem));
                return;
            }
    
            ProcessResults(_results);
        }
    
        private void OnGetDownloadedStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null && !string.IsNullOrEmpty(e.Result))
            {
                // do something with e.Result string.
                _results.Add(e.Result);
            }
            DownloadItem();
        }
    

    Edit:
    I’ve modified your code to use a Queue. Not entirely sure how you wanted progress to work. I’m sure if you wanted the progress to cater for all downloads, then you could store the item count in the ‘PopulateItemsQueue()’ method and use that field in the progress changed method.

        private Queue<string> _downloadUrls = new Queue<string>();
    
        private void downloadFile(IEnumerable<string> urls)
        {
            foreach (var url in urls)
            {
                _downloadUrls.Enqueue(url);
            }
    
            // Starts the download
            btnGetDownload.Text = "Downloading...";
            btnGetDownload.Enabled = false;
            progressBar1.Visible = true;
            lblFileName.Visible = true;
    
            DownloadFile();
        }
    
        private void DownloadFile()
        {
            if (_downloadUrls.Any())
            {
                WebClient client = new WebClient();
                client.DownloadProgressChanged += client_DownloadProgressChanged;
                client.DownloadFileCompleted += client_DownloadFileCompleted;
    
                var url = _downloadUrls.Dequeue();
                string FileName = url.Substring(url.LastIndexOf("/") + 1,
                            (url.Length - url.LastIndexOf("/") - 1));
    
                client.DownloadFileAsync(new Uri(url), "C:\\Test4\\" + FileName);
                lblFileName.Text = url;
                return;
            }
    
            // End of the download
            btnGetDownload.Text = "Download Complete";
        }
    
        private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                // handle error scenario
                throw e.Error;
            }
            if (e.Cancelled)
            {
                // handle cancelled scenario
            }
            DownloadFile();
        }
    
        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;
            progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class whose new method has been made private because I want
I have been searching on this but it is surprisingly hard to come by
I find it surprising that this has not been asked yet, so I am
It has been a while since I have wrote any code due to military
It has been a while since I messed with C code. I am getting
This has been asked in few times in this thread. But sometimes its hard
Surprisingly I've been unable to find anyone else really doing this, but surely someone
Even though the API has been open since Mac OS X Leopard, there's surprisingly,
This has been a problem that I haven't been able to figure out for
There has been some talk of Website performance monitoring tools and services on stackoverflow,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.