/// <summary>
        /// Performs the BackgroundDownloadOperation.
        /// </summary>
        public async Task <LiveOperationResult> ExecuteAsync()
        {
            Debug.Assert(this.status != OperationStatus.Completed, "Cannot execute on a completed operation.");

            var builder = new BackgroundDownloadRequestBuilder
            {
                AccessToken = this.accessToken,
                DownloadLocationOnDevice = this.downloadLocationOnDevice,
                RequestUri          = this.requestUri,
                TransferPreferences = this.transferPreferences
            };

            this.request = builder.Build();
            var eventAdapter = new BackgroundDownloadEventAdapter(this.backgroundTransferService, this.tcs);

            Task <LiveOperationResult> task = this.progress == null?
                                              eventAdapter.ConvertTransferStatusChangedToTask(this.request) :
                                                  eventAdapter.ConvertTransferStatusChangedToTask(this.request, this.progress);

            Debug.Assert(this.tcs.Task == task, "EventAdapter returned a different task. This could affect cancel.");

            // if the request has already been cancelled do not add it to the service.
            if (this.status != OperationStatus.Cancelled)
            {
                this.backgroundTransferService.Add(this.request);
                this.status = OperationStatus.Started;
            }

            LiveOperationResult result = await task;

            this.status = OperationStatus.Completed;
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attaches to the pending download and receive the result of the operation when it is finished.
        /// </summary>
        /// <param name="ct">a token that is used to cancel the background download operation.</param>
        /// <param name="progress">an object that is called to report the background download's progress.</param>
        /// <returns>A Task object representing the asynchronous operation.</returns>
        public Task <LiveOperationResult> AttachAsync(CancellationToken ct, IProgress <LiveOperationProgress> progress)
        {
            var tcs = new TaskCompletionSource <LiveOperationResult>();

            ct.Register(() =>
            {
                // remove from service to cancel.
                this.backgroundTransferService.Remove(this.request);
                tcs.TrySetCanceled();
            });

            var downloadEventAdapter = new BackgroundDownloadEventAdapter(this.backgroundTransferService, tcs);

            return(downloadEventAdapter.ConvertTransferStatusChangedToTask(this.request, progress));
        }
        /// <summary>
        /// Performs the BackgroundDownloadOperation.
        /// </summary>
        public async Task<LiveOperationResult> ExecuteAsync()
        {
            Debug.Assert(this.status != OperationStatus.Completed, "Cannot execute on a completed operation.");

            var builder = new BackgroundDownloadRequestBuilder
            {
                AccessToken = this.accessToken,
                DownloadLocationOnDevice = this.downloadLocationOnDevice,
                RequestUri = this.requestUri,
                TransferPreferences = this.transferPreferences
            };

            this.request = builder.Build();
            var eventAdapter = new BackgroundDownloadEventAdapter(this.backgroundTransferService, this.tcs);

            Task<LiveOperationResult> task = this.progress == null ?
                                             eventAdapter.ConvertTransferStatusChangedToTask(this.request) :
                                             eventAdapter.ConvertTransferStatusChangedToTask(this.request, this.progress);

            Debug.Assert(this.tcs.Task == task, "EventAdapter returned a different task. This could affect cancel.");

            // if the request has already been cancelled do not add it to the service.
            if (this.status != OperationStatus.Cancelled)
            {
                this.backgroundTransferService.Add(this.request);
                this.status = OperationStatus.Started;
            }

            LiveOperationResult result = await task;
            this.status = OperationStatus.Completed;
            return result;
        }