0

I am making this application where I have to download multiple files from my website according to whether a checkbox is checked or not. I am using DownloadFileAsync method for downloading the files.

The problem I have is that, once the download starts for the first file. It continues on with rest of the code. For example. It will add "1" to the listbox before the download even finishes and then also move on to next if statement and execute the download for it as well and add "2" to listbox as soon as the download starts. Below is the code I am using.

private void button1_Click(object sender, EventArgs e)
        {

            if (checkBox1.Checked)
            {
                WebClient client = new WebClient();
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
                client.DownloadFileAsync(new Uri("https://speed.hetzner.de/100MB.bin"), "100mb");
                listBox1.Items.Add("1");
            }
             if (checkBox2.Checked)
            {
                WebClient client = new WebClient();
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
                client.DownloadFileAsync(new Uri("https://speed.hetzner.de/100MB.bin"), "200mb");
                listBox1.Items.Add("2");
            }
        }


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

        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            progressBar1.Value = 0;
        }

I tried using async and wait but I couldn't get it to work. In short, how can I make the code download the first file completely, then add "1" to the listbox and only then move to the second if statement to download second file.

Thanks in advance.

  • If you want sequential download while still using events such as `DownloadProgressChanged `, you have to use the async/non-blocking download methods. But you cannot just proceed starting the following downloads after you have started the first download. (It should be rather obvious why.) Only start the 2nd download when the 1st finished (and only start the 3rd when the 2nd download completed, etc...) How to do that? Well, make use of the DownloadFileCompleted event (which you are already familiar with, since you are already using it in your code) –  May 25 '19 at 22:02
  • In the real application, I will have more than 2 check boxes to check. how can I check for next check box without going through the first one again after each download. If that makes sense. Also, I have to add items in list box according to the check box. So I am not sure how the DownloadFileCompleted event will help me do all that. Thanks for your reply! – Jahanzaib Bokhari May 25 '19 at 22:11
  • I suggest to start with building a list (rather something like a fifo Queue, i guess) with all the download jobs (i.e., the files to download). Then, proceeding with the actual download logic, pull (take and remove) the first download item from that list and start the download. Then, in the DownloadFileCompleted event, pull the next download item and re-initiate the download (unless the list has become empty and there is nothing more to download, of course). –  May 25 '19 at 22:15
  • I can see that working, would you have an example of that by any chance. – Jahanzaib Bokhari May 25 '19 at 22:23
  • No, i don't have an example ready. Just the general concept/approach from the top of my head. But since it is rather simple, i don't expect it being too difficult for you to implement (i hope). –  May 25 '19 at 22:25
  • ya, doesn't seem hard. Just haven't done it before. Thanks though. Will give it a try! – Jahanzaib Bokhari May 25 '19 at 22:34

0 Answers0