0

我正在使用WebClient该类的多个实例,以便同时从 Internet 上的 Web 服务器异步下载许多小文件。
我使用的 URI 的 IP 采用数字表示法,以避免不必要的 DNS 解析。

我正在推动它,例如同时使用 50 多个该类的实例。

我注意到虽然大多数文件都正确下载,但其中一小部分的长度为 0,这意味着该线程从未成功完成——可能是由于错误。
但是AsyncCompletedEventArgs从未报告错误。

我是不是推得太多了?但是话又说回来,它不应该发出错误吗?

4

1 回答 1

1

如果有人遇到这个非常老的问题,最好的猜测是在下载完成之前线程已经被杀死,导致文件大小等于 0。

例如,让我们比较一下

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient client = new WebClient();
            client.DownloadFileAsync(new Uri("http://ipv4.download.thinkbroadband.com/5MB.zip"), "C:\\5MB.zip");
            Thread.Sleep(30000);
        }
    }
}

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient client = new WebClient();
            client.DownloadFileAsync(new Uri("http://ipv4.download.thinkbroadband.com/5MB.zip"), "C:\\5MB.zip");
        }
    }
}

在第二种情况下,一旦开始下载,程序就会退出,导致下载中断,这表现为在 C: 驱动器上创建大小为 0 的文件。

于 2016-02-09T22:14:57.513 回答