none
How to Async download multiple files using webclient simultaneously?

    Question

  • i want to download multiple file and want to show multiple progress bar for each file download progress.i got a code which can download file at a time but i want to start download many files at a time and also like to show progress bar for each file progress

    suppose i have one textbox where i will put url and when i click on download button then download will start and a progress bar will be show for showing that file download progress. the same if i put another url in textbox and click on download button then another file download start and another progress bar will be shown to show that file download progress. i want that each file will be downloading in separate thread means my program will be multi threaded.

    the code i have for downloading one file at a time.please guide me what to change in code to achieve my goal. thanks

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

    Wednesday, March 27, 2013 6:00 PM

Answers

  • When you are using keywords async/await then you are running that specific code on another thread. That means that your code will not block Main thread. 

    If you look at this picture (which is acutally from the MSDN documentation):

    then you will understand everything. Just follow the numbers and you will understand what is your code doing.


    Microsoft Student Parnter Microsoft Technology Associate

    • Marked as answer by Mou_kolkata Friday, March 29, 2013 11:34 AM
    Thursday, March 28, 2013 8:00 PM
  •   private async 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));
    
              await  client.DownloadFileAsync(new Uri(url), "C:\\Test4\\" + FileName);
                lblFileName.Text = url;
                return;
            }
    
            // End of the download
            btnGetDownload.Text = "Download Complete";
        }
    Just add the two words Aynch and Await as shown above.

    JP Cowboy Coders Unite!

    • Marked as answer by Mou_kolkata Monday, January 13, 2014 9:14 AM
    Thursday, March 28, 2013 5:56 PM

All replies

  •   private async 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));
    
              await  client.DownloadFileAsync(new Uri(url), "C:\\Test4\\" + FileName);
                lblFileName.Text = url;
                return;
            }
    
            // End of the download
            btnGetDownload.Text = "Download Complete";
        }
    Just add the two words Aynch and Await as shown above.

    JP Cowboy Coders Unite!

    • Marked as answer by Mou_kolkata Monday, January 13, 2014 9:14 AM
    Thursday, March 28, 2013 5:56 PM
  • sorry but u did not explain the usage of this 2 keyword. Async and Await

    can u please what usage of Async and Await keyword in details. thanks

    Thursday, March 28, 2013 6:19 PM
  • When you are using keywords async/await then you are running that specific code on another thread. That means that your code will not block Main thread. 

    If you look at this picture (which is acutally from the MSDN documentation):

    then you will understand everything. Just follow the numbers and you will understand what is your code doing.


    Microsoft Student Parnter Microsoft Technology Associate

    • Marked as answer by Mou_kolkata Friday, March 29, 2013 11:34 AM
    Thursday, March 28, 2013 8:00 PM
  • Really nice.. Thanks :)
    Monday, January 13, 2014 8:18 AM