0

I am creating a file downloader in wpf which downloads from a list of files given to it and shows a progress bar for the individual download going on....the list may be upto 1000 files.

I have got all the code and specifics worked out but am stuck on one thing....how to queue the files. Consider the code below :

 private bool Download(String url, int i)
        {
            String FileName = "some location.mp4";
            label2.Content = "Downloading : " + ht[current.ToString()].ToString();
            progressBar1.Value = 0;            
            try
            {
                if (url.Equals(Constants.NotFound))
                    return false;

                if (File.Exists(FileName))
                {                      
                    current++;
                    if (current <= total)                                                
                        Download(ht[current.ToString()].ToString(), current);
                    else
                    {
                        this.WindowState = System.Windows.WindowState.Normal;
                        MessageBox.Show("Download Finished");
                    }

                    return true;
                }  
                wc.DownloadFileAsync(new Uri(url), FileName);                   
                return true;
            }
            catch
            { return false; }
        }

To capture the complete event, i have written the event complete handler as so :

void wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        current++;            
        if (current <= total)
            Download(ht[current.ToString()].ToString(), current);
        else
        {
            progressBar1.Value = 100;                
            label2.Content = "Download Finished.";
            label1.Content = "100 %";

            this.WindowState = System.Windows.WindowState.Normal;
            MessageBox.Show("Download Finished");
        }
    }

this works perfectly when none of the files being downloaded already exist but when the files pre-exist then the download function becomes a recursive loop and does not return a value till the subsequent function calls return and considering the 1000's of videos, it could be huge on the memory too.

So any way to avoid this/get over it or maybe a different approach then i am using ??

and ya, thanks in advance....:)

1 Answers1

0

You have recursion and duplicate code. Try this:

            if (File.Exists(FileName))
            {                      
                return true;
            }
            else 
            {
                wc.DownloadFileAsync(new Uri(url), FileName);                   
                return true;
            }
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • hey @blam, thanks for your reply.....i now about the duplicate code but i cant use the coode you mentioned bcos as you can see the next file is called for download from the event complete handler and i need to call that....using return true if file exists will just terminate the code then and there....hence the duplicate code.... – user1260042 Mar 10 '12 at 05:58