我使用Webclient異步下載4個文件。問題是,在這個情景中,當任何文件完成時,它不會觸發FileCompletedEvent。DownloadFileCompleted事件不工作,而異步使用webclient下載文件
不要擔心代碼,我已經刪除了很多額外的代碼。它必須在一個時間
Dictionary<int, WebClient> wd = new Dictionary<int, WebClient>();
public void DownloadPDF(string url,string path)
{
index=1;
if (wd.ContainsKey(1) == false)
{
wd.Add(1, null);
}
for (int i = 1; i <=5; i++)
{
if (wd[i] == null)
{
wd[i] = new WebClient();
index = i;
break;
}
}
wd[index].DownloadFileCompleted += (sender, e) => w_DownloadFileCompleted(sender, e, path,index);
wd[index].DownloadFileAsync(new Uri(url), path);
}
private void w_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e, string path,int index)
{
wd[index] = null;
}
這個問題不同於其他在下載併發的情況下的情況下,對單個文件文件。謝謝
@ LasseV.Karlsen對不起,這已經是本地的了。我只是在處理代碼時錯位了行的位置 – Charlie 2015-02-12 06:51:19