DownloadFileAsync问题 [英] DownloadFileAsync Problem
问题描述
我有以下代码.它会下载FilesToDownload列表中的所有文件.我想使用DownloadFileAsync,因为这样我就可以使用进度条.但是,通过DownloadFileAsync,它将下载第一个文件并停止.如果我将其更改为DownloadFile,则可以正常工作,但不会显示进度.所以我的问题是如何多次使用DownloadFileAsync?完成后是否必须使用CancelAsync,然后重新启动?感谢您的帮助.
I have the following code. It downloads all the files in the list FilesToDownload. I want to use DownloadFileAsync because then I can use the the progressbar. However with DownloadFileAsync it will download the first file and stop. If I change it to DownloadFile it works fine but no progress is displayed. So my question is how do you use DownloadFileAsync multiple times? Do I have to use CancelAsync when its done and then start again? Any help is appreciated.
private void btnDownload_Click(object sender, RoutedEventArgs e)
{
choiceGrid.Visibility = Visibility.Hidden;
if (FilesToDownload.Count > 0)
{
statusGrid.Visibility = Visibility.Visible;
int updatesSelected = FilesToDownload.Count;
int updateCounter = 0;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(Properties.Settings.Default.optUpdatePath);
WebClient clientDownloader = new WebClient();
clientDownloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(clientDownloader_DownloadProgress);
clientDownloader.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(clientDownloader_DownloadCompleted);
bool downloaded = false;
bool found = false;
foreach (String filePath in FilesToDownload)
{
foreach (XmlNode xNode in xDoc.DocumentElement.ChildNodes)
{
if (!found)
{
if (xNode.Name == "Picture")
{
foreach (XmlNode xNode2 in xNode.ChildNodes)
{
if (xNode2.Name == "Path")
{
if (xNode2.InnerText == filePath)
{
found = true;
try
{
if (!File.Exists(Properties.Settings.Default.contentFilePath + Path.GetFileName(filePath)))
{
lblFileCount.Content = "Working on update " + (updateCounter + 1) + " of " + updatesSelected;
lblActiveFile.Content = filePath;
clientDownloader.DownloadFile(new Uri(filePath), Properties.Settings.Default.contentFilePath + Path.GetFileName(filePath));
downloaded = true;
}
}
catch
{
downloaded = false;
}
}
if (downloaded)
{
xNode2.InnerText = Properties.Settings.Default.contentFilePath + Path.GetFileName(filePath);
AddUpdateNode(xNode);
updateCounter++;
}
}
}
}
}
downloaded = false;
}
found = false;
}
clientDownloader.Dispose();
lblActiveFile.Content = "Finished Downloading Updates!";
lblFileCount.Content = updateCounter + " of " + updatesSelected + " updates have been downloaded and installed.";
FilesToDownload.Clear();
}
else
{
MessageBox.Show("Nothing was selected to download.");
}
}
void clientDownloader_DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
prgBar.Value = e.ProgressPercentage;
}
void clientDownloader_DownloadCompleted(object sender, AsyncCompletedEventArgs e)
{
prgBar.Value = 100;
}
推荐答案
if (FilesToDownload.Count > 0)
{
and end of the if statement place in a new method which you will call on different threads for each file that you want to download.
and end of the if statement place in a new method which you will call on different threads for each file that you want to download.
Have a look at this example I wrote:
Have a look at this example I wrote:
http://code.msdn.microsoft.com/CCS-LABS-Windows-Forms-Web-4a18db8d
这篇关于DownloadFileAsync问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!