我正在创建一个文件下载器在wpf中,下载从一个文件列表给它,并显示一个进度条为个人下载去on....the列表可能多达1000个文件。
我已经得到了所有的代码和细节的工作,但卡在一个thing....how队列的文件。考虑下面的代码:
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; }
}为了捕获完整的事件,我编写了事件完成处理程序,如下所示:
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");
}
}当下载的文件都不存在时,这是完美的,但是当文件预先存在时,则下载函数变成递归循环,并且直到后续函数调用返回时才返回值,并且考虑到1000个视频,它在内存上也可能是巨大的。
所以,有没有办法避免/克服它,或者可能是我正在使用的另一种方法??
还有,提前谢谢你……:)
发布于 2012-03-10 03:53:18
你有递归和重复的代码。试试这个:
if (File.Exists(FileName))
{
return true;
}
else
{
wc.DownloadFileAsync(new Uri(url), FileName);
return true;
}https://stackoverflow.com/questions/9639825
复制相似问题