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....:)