I have a WebClient
downloading a .chm file (as seen in the code below). It seems to be very irregular in what it is downloading. The full file size is around 2500-2600 KB but about 50-75% of the time I get files back that are smaller (some examples: 1233 KB, 657 KB, 353 KB, 1745 KB, etc.).
(Code is simplified/personal details removed)
public static void DownloadMyFile(string destFileAndPath)
{
//Get base for help Url, stop at first "/" ignoring "https://", then add path in server
string Url = "https://mywebservice.com/myFile.chm";
using (var client = new WebClient())
{
//I need to do stuff to the downloaded file when done
client.DownloadFileCompleted += client_DownloadFileCompleted;
client.DownloadFileAsync(new Uri(Url), destFileAndPath);
//More waiting?
while (client.IsBusy) { }
}
}
And the Event, which is working fine, but on an "unfinished" file:
public static void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
//Do stuff like comparing the file to another, renaming, copying, etc.
}
Am I really missing something here?
It looks like Paul Roub's comment led me to the correct solution. I was only checking against the e.Cancelled
property.
Occasionally, there were exceptions thrown (mostly a WebException
of some kind) that would cause the download to error. Once I checked the e.Error
property, I was able to either retry or handle the failure for my situation.