2

I have another question :(.
I'm trying to download multiple files for my application.

My question is: What do I have to do to check if the first download is done and then continue to the second download and so on?

This is my code atm:

        private void DownloadBukkit()
        {
            MySingleton.Instance.FirstStartProgress = "Downloading Bukkit.jar... Please stand by...";
            webClient.DownloadFileAsync(new Uri(MySingleton.Instance.BukkitDownloadLink), Jar_Location);
            webClient.DownloadProgressChanged += backgroundWorker1_ProgressChanged;
            webClient.DownloadFileCompleted += (webClient_DownloadFileCompleted);
        }

        private void DownloadDll()
        {
            if (!webClient.IsBusy)
            {
                MySingleton.Instance.FirstStartProgress = "Downloading HtmlAgilityPack.dll... Please stand by...";
                webClient2.DownloadFileAsync(new Uri(Dll_HtmlAgilityPackUrl), Dll_HtmlAgilityPackLocation);
                webClient2.DownloadProgressChanged += backgroundWorker1_ProgressChanged;
                webClient2.DownloadFileCompleted += (webClient2_DownloadFileCompleted);
            }
        }

    void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
            DownloadDll();
    }


    void webClient2_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
         Invoke((MethodInvoker)
               Close);
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Invoke((MethodInvoker)
                   delegate
                       {
                           labelProgress.Text = MySingleton.Instance.FirstStartProgress;
                           progressBar1.Value = e.ProgressPercentage;
                       });
        }

I have checked this link: DownloadFileAsync multiple files using webclient but I didn't really understand how to implant this :(. (I'm quite new to C#)

Community
  • 1
  • 1
Yuki Kutsuya
  • 3,968
  • 11
  • 46
  • 63

2 Answers2

3

The DownloadFileCompleted event is your signal to know that you are finished downloading the file.

From MSDN:

This event is raised each time an asynchronous file download operation completes.

It's not clear from your code, where and how webClient and webClient2 are declared but essentially, you can start your second download when the first DownloadFileCompleted event is fired. Note, however, that you can perform the download of 2 different files concurrently provided you use 2 separate instances of WebClient.

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • Okai thx! So that means I just have to put a new webclient in each DownloadFileCompleted event? Edit: I have tried to start the second download in the DownloadfileCompleted event, but that doesn't seem to work though. – Yuki Kutsuya Apr 02 '12 at 14:53
  • @Darkshadw I expanded a little bit my answer. I hope that makes it clear. – Icarus Apr 02 '12 at 14:59
  • #Icarus Whenever I try to do this: `WebClient webClient2 = new WebClient(); MySingleton.Instance.FirstStartProgress = "Downloading Dll... Please stand by..."; webClient2.DownloadFileAsync(new Uri(Dll_HtmlAgilityPackUrl), Dll_HtmlAgilityPackLocation);` It doesn't want to start the download :(. – Yuki Kutsuya Apr 02 '12 at 15:10
0

Here is the fast forward code you can change it as per your requirement .

 WebClient client = null;
        public FileDownloader()
        {
            InitializeComponent();
            client = new WebClient();
            client.DownloadProgressChanged += client_DownloadProgressChanged;
            client.DownloadFileCompleted += client_DownloadFileCompleted;
        }

        void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            lblMessage.Text = "File Download Compeleted.";
        }

        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            lblMessage.Text = e.ProgressPercentage + " % Downloaded.";
        }

        private void StartDownload(object sender, RoutedEventArgs e)
        {
            if (client == null)
                client = new WebClient();

//Loop thru your files for eg. file1.dll, file2.dll .......etc.
            for (int index = 0; index < 10; index++)
            {
                //loop on files 
                client.DownloadFileAsync(
                    new Uri(
                            @"http://mywebsite.com/Files/File" + index.ToString() + ".dll"
                            , UriKind.RelativeOrAbsolute),
                    @"C:\Temp\file+" + index.ToString() + ".dll");
            }

        }
JSJ
  • 5,653
  • 3
  • 25
  • 32