WebClient.DownloadFileAsync - 下载文件,一次一个 [英] WebClient.DownloadFileAsync - Download files one at a time

查看:261
本文介绍了WebClient.DownloadFileAsync - 下载文件,一次一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code下载多个附件从TFS服务器:

I am using the code below to download multiple attachments from a TFS server:

foreach (Attachment a in wi.Attachments)
{    
    WebClient wc = new WebClient();
    wc.Credentials = (ICredentials)netCred;
    wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
    wc.DownloadFileAsync(a.Uri, "C:\\" + a.Name);
}

我想下载使用DownloadFileAsync多个文件,但我希望他们可以下载一个接一个。

I would like to download multiple files using DownloadFileAsync, but I want them to be downloaded one by one.

有人可能会问:为什么你不只是使用同步DownloadFile方法?其原因是:

One may ask "Why don't you just use the synchronous DownloadFile method?" Its because:


  1. 我要使用由DownloadFileAsync提供的事件。

  2. 我不想让Web客户端的多个实例,避免泛滥的服务器。

这是我想到的解决办法:

This is the solution that I thought of:

foreach (Attachment a in wi.Attachments)
{        
    WebClient wc = new WebClient();
    wc.Credentials = (ICredentials)netCred;
    wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
    wc.DownloadFileAsync(a.Uri, "C:\\" + a.Name);
    while (wc.IsBusy)
    {
        System.Threading.Thread.Sleep(1000);
    }
}

但是,也有一对这种方法的问题:

However, there are a couple of problems with this approach:


  1. Thread.sleep代码()是我的形式锁定。我还需要使自己的线程或使用的BackgroundWorker。 (我想避免这种情况,尽可能)

  2. 所有的文件已被下载后,被触发的事件DownloadFileCompleted。我不知道这是否是使用System.Threading.Thread.Sleep(1000)的副作用;

  1. The Thread.Sleep() is locking up my Form. I still need to make my own Thread or use BackgroundWorker. (I would like to avoid this as much as possible)
  2. The DownloadFileCompleted event is being triggered after ALL files has been downloaded. I don't know if this is a side-effect of using System.Threading.Thread.Sleep(1000);

有没有更好的办法利用WebClient.DownloadFileAsync下载文件一次?

Is there a better approach to download files one at a time using WebClient.DownloadFileAsync?

谢谢!

推荐答案

要简化你可以创建分离附件列表中的任务:

To simplify the task you can create separated attachment list:

list = new List<Attachment>(wi.Attachments);

其中列表是键入列表与LT私人领域;附件&GT;
在这之后,你应该配置Web客户端,并启动第一个文件的下载:

where list is private field with type List<Attachment>. After this you should configure WebClient and start downloading of first file:

if (list.Count > 0) {
   WebClient wc = new WebClient();
   wc.Credentials = (ICredentials)netCred;
   wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
   wc.DownloadFileAsync(list[0].Uri, @"C:\" + list[0].Name);
}

您DownloadFileComplete处理程序应检查如果不是所有的文件已经下载并再次呼吁DownloadFileAsync:

Your DownloadFileComplete handler should check if not all files already downloaded and call DownloadFileAsync again:

void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) {
   // ... do something useful 
   list.RemoveAt(0);
   if (list.Count > 0)
      wc.DownloadFileAsync(list[0].Uri, @"C:\" + list[0].Name);
}

这code未优化的解决方案。这只是想法。

This code is not optimized solution. This is just idea.

这篇关于WebClient.DownloadFileAsync - 下载文件,一次一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆