public static Task<string> DownloadAsync (Update update, IProgress<int> progress) { return DownloadAsync (update, progress, CancellationToken.None); }
public static Task<string> DownloadAsync (Update update, IProgress<int> progress, CancellationToken cancelToken) { if (update == null) throw new ArgumentNullException ("update"); if (progress == null) throw new ArgumentNullException ("progress"); TaskCompletionSource<string> tcs = new TaskCompletionSource<string>(); string path = Path.Combine (Path.GetTempPath(), "GablarskiUpdate.exe"); WebClient web = new WebClient(); web.DownloadFileCompleted += (sender, args) => { if (args.Cancelled) tcs.SetCanceled(); else if (args.Error != null) tcs.SetException (args.Error); else tcs.SetResult (path); }; web.DownloadProgressChanged += (sender, args) => progress.Report (args.ProgressPercentage); web.DownloadFileAsync (new Uri (update.InstallerUrl), path); cancelToken.Register (web.CancelAsync); return tcs.Task; }