How to download multiple files in C# from array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dancgr
    New Member
    • Jan 2010
    • 8

    How to download multiple files in C# from array.

    I have an array with filenames, and i want to download them from the server, but once i start a download, it must finish before i star a new one. I don't know how to make it wait, provided that the loop is inside a Download_COmple ted event function...

    Here is the code:
    Code:
                    while (l <= j-1)
                            {
                                label1.Text = "Baixando " + download[l] + "...";
                                client2.DownloadFileAsync(new Uri(url + download[l]), download[l]);
                                client2.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                                l++;
                            }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I am WAY out of my area here but...
    Line 4 looks like an Asynchronous download method.
    Is there one just like it that doesn't have the Aysnc? Just "DownloadFi le"

    Comment

    • dancgr
      New Member
      • Jan 2010
      • 8

      #3
      Yes, but that one doesn't connect with the Async progress bar...

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        but once i start a download, it must finish before i star a new one.
        Yes, but that one doesn't connect with the Async progress bar...
        Then use a different progress bar.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Have you tried creating a new webclient object?
          The Async part allows you to download without blocking the thread, so make another one and try downloading a 2nd file.
          If it works (and doesn't cause memory/io issues) expand from there.
          I would not recomend doing more then 2 downloads at once though

          Comment

          • dancgr
            New Member
            • Jan 2010
            • 8

            #6
            No, i don't want to do them at the same time, i want to do them one after one.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Well ok I misread it, then whats the issue?
              When the download completed event is fired, start the next one?

              Comment

              • dancgr
                New Member
                • Jan 2010
                • 8

                #8
                Any idea why it tries to download more than one file at once?

                It is not suposed to, but sometimes, at random, it ignores the loop in the end and start a new download while another one is in progress and craches the program.

                Code:
                                while (l < i)
                                {
                                    label1.Text = "Baixando " + download[l] + "...";
                                    client2.DownloadFileAsync(new Uri(url + download[l]), download[l]);
                                    client2.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                                    while (progressBar1.Value < 100)
                                    {
                                        Application.DoEvents();
                                        label4.Text = progressBar1.Value.ToString() + " %";
                                    }
                                    l++;
                                    progressBar1.Value = 0;
                               }
                Any sugestion?

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Why are you not doing the progressbar stuff in the downloadprogres schanged event?
                  You probably should not use a while loop then, but instead pass the index around in the event handlers and when the download complete happens, you can pass the next index into a download again

                  Comment

                  • dancgr
                    New Member
                    • Jan 2010
                    • 8

                    #10
                    But the while loop is already inside the Download Completed Event.

                    The dowloads loop occurs after another one.
                    Last edited by tlhintoq; Jan 27 '10, 04:26 AM. Reason: "inside" not "insite" makes more sense

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      Code:
                      while (l < i)
                                      {
                                          label1.Text = "Baixando " + download[l] + "...";
                                          client2.DownloadFileAsync(new Uri(url + download[l]), download[l]);
                                          client2.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                                          while (progressBar1.Value < 100)
                                          {
                                              Application.DoEvents();
                                              label4.Text = progressBar1.Value.ToString() + " %";
                                          }
                                          l++;
                                          progressBar1.Value = 0;
                                     }
                      Did you realize that every time you go through the outter loop (L) that you are attaching another response to the client2.Downloa dProgressChange d event? (Line 5)

                      The first time through the outer loop the handler will be attached (+=) and so the method will be called one time for every DownloadProgres sChanged event.

                      The second time through the outer loop the same method will be attached to the same event AGAIN. So when the DownloadProgres sChanged event is raised the handler method will be notified TWICE.

                      and so on. Maybe the weird behavior you are seeing is because the file isn't done actually done downloading when the progressbar reaches a value of 100

                      RULE: For every attachment (+=) of an event to a method handler you must also detach (-=) the handler from the event. If you don't you will get unexpected results and it will slowly consume memory. Disposing of a class without detaching event handlers will create a memory leak because their is no longer a way to reference the connection to disconnect it.

                      Try something like this
                      Code:
                      client2.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                      // Move the attachment to before the loop so it only happens once.
                                      while (l < i)
                                      {
                                          label1.Text = "Baixando " + download[l] + "...";
                                          client2.DownloadFileAsync(new Uri(url + download[l]), download[l]);
                                          while (progressBar1.Value < 100)
                                          {
                                              Application.DoEvents();
                                              label4.Text = progressBar1.Value.ToString() + " %";
                                          }
                                          l++;
                                          progressBar1.Value = 0;
                                     }
                      client2.DownloadProgressChanged -= new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                      // Detach the handler from the event when you are done with it.

                      Comment

                      Working...