public static async Task<string> LoadAsyncTo(string argument, string destination, IProgress<AsyncProgressEntry> progress = null,
CancellationToken cancellation = default(CancellationToken)) {
var loader = CreateLoader(argument);
try {
// TODO: Timeout?
using (var client = new CookieAwareWebClient {
Headers = {
[HttpRequestHeader.UserAgent] = CmApiProvider.UserAgent
}
}) {
progress?.Report(AsyncProgressEntry.Indetermitate);
cancellation.ThrowIfCancellationRequested();
cancellation.Register(client.CancelAsync);
if (!await loader.PrepareAsync(client, cancellation) ||
cancellation.IsCancellationRequested) return null;
var skipEvent = 0;
client.DownloadProgressChanged += (sender, args) => {
if (++skipEvent > 50) {
skipEvent = 0;
} else {
return;
}
var total = args.TotalBytesToReceive;
if (total == -1 && loader.TotalSize != -1) {
total = Math.Max(loader.TotalSize, args.BytesReceived);
}
// ReSharper disable once AccessToDisposedClosure
progress?.Report(AsyncProgressEntry.CreateDownloading(args.BytesReceived, total));
};
await loader.DownloadAsync(client, destination, cancellation);
if (cancellation.IsCancellationRequested) return null;
Logging.Debug("Loaded: " + destination);
}
return destination;
} catch (TaskCanceledException) {
return null;
} catch (Exception e) {
NonfatalError.Notify(ToolsStrings.Common_CannotDownloadFile, ToolsStrings.Common_CannotDownloadFile_Commentary, e);
return null;
}
}
}