コード例 #1
0
ファイル: Updater.cs プロジェクト: ermau/Gablarski
		public static Task<string> DownloadAsync (Update update, IProgress<int> progress)
		{
			return DownloadAsync (update, progress, CancellationToken.None);
		}
コード例 #2
0
ファイル: Updater.cs プロジェクト: ermau/Gablarski
		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;
		}