WebClient.DownloadFileAsync: when downloading a lot of files
I am using multiple instances of a class WebClient
to download multiple small files at the same time asynchronously from a web server on the internet.
The URI I am using has an IP number in the notation to avoid unnecessary DNS resolutions.
I nudge it a bit by using, for example, 50+ instances of the class at the same time.
I noticed that while most of the files uploaded correctly, a small percentage of them are 0 in length, which means the stream never completed successfully - possibly due to an error.
However, AsyncCompletedEventArgs
never reported a bug.
Am I pushing him too hard? But then again, shouldn't this be a mistake?
In case anyone hits this really old question, it is best to assume that the thread was killed before the download was complete, which results in a file with size 0.
For example, compare what's going on between
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);
}
}
}
and
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");
}
}
}
In the second case, as soon as the download starts, the program will exit, which will result in an interrupted download, which manifests itself in a file created on the C: drive with a size of 0.
source to share