/// <summary> /// Download a file into a stream. /// </summary> /// <param name="path">relative or absolute uri to the file to be downloaded.</param> /// <param name="ct">a cancellation token</param> /// <param name="progress">a progress event callback handler</param> public Task <LiveDownloadOperationResult> DownloadAsync( string path, CancellationToken ct, IProgress <LiveOperationProgress> progress) { LiveUtility.ValidateNotNullOrWhiteSpaceString(path, "path"); var tcs = new TaskCompletionSource <LiveDownloadOperationResult>(); var op = new DownloadOperation( this, this.GetResourceUri(path, ApiMethod.Download), progress, null); op.OperationCompletedCallback = (LiveDownloadOperationResult result) => { if (result.IsCancelled) { tcs.TrySetCanceled(); } else if (result.Error != null) { tcs.TrySetException(result.Error); } else { tcs.TrySetResult(result); } }; ct.Register(op.Cancel); op.Execute(); return(tcs.Task); }
/// <summary> /// Download a file into a stream. /// </summary> /// <param name="path">relative or absolute uri to the file to be downloaded.</param> /// <param name="ct">a token that is used to cancel the download operation.</param> /// <param name="progress">an object that is called to report the download's progress.</param> /// <returns>A Task object representing the asynchronous operation.</returns> public Task <LiveDownloadOperationResult> DownloadAsync( string path, CancellationToken ct, IProgress <LiveOperationProgress> progress) { if (string.IsNullOrEmpty(path)) { string message = String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("UrlInvalid"), "path"); throw new ArgumentException(message, "path"); } if (this.Session == null) { throw new LiveConnectException(ApiOperation.ApiClientErrorCode, ResourceHelper.GetString("UserNotLoggedIn")); } var tcs = new TaskCompletionSource <LiveDownloadOperationResult>(); var operation = new DownloadOperation( this, this.GetResourceUri(path, ApiMethod.Download), progress != null ? new Action <LiveOperationProgress>(progress.Report) : null, SynchronizationContextWrapper.Current) { OperationCompletedCallback = (LiveDownloadOperationResult result) => { if (result.IsCancelled) { tcs.TrySetCanceled(); } else if (result.Error != null) { tcs.TrySetException(result.Error); } else { tcs.TrySetResult(result); } } }; ct.Register(operation.Cancel); operation.Execute(); return(tcs.Task); }