【问题标题】:DownloadFileCompleted event not working while asynchronously downloading files using webclient使用 webclient 异步下载文件时,DownloadFileCompleted 事件不起作用
【发布时间】:2015-02-12 06:41:55
【问题描述】:

我正在使用 Webclient 异步下载 4 个文件。问题是在这种情况下,当任何文件完成时,它都不会触发 FileCompletedEvent。

不用担心代码,我已经删除了很多额外的代码。它必须一次只用于单个文件

 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 对不起,这已经是本地的了。我只是在处理代码时放错了行的位置

标签: c# webclient


【解决方案1】:

你尝试过 AsyncCompletedEventHandler 吗?

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
相关资源
最近更新 更多