public AttachAsync ( ) : DownloadOperation>.IAsyncOperationWithProgress |
||
Результат | DownloadOperation>.IAsyncOperationWithProgress |
private async Task HandleDownloadAsync(DownloadOperation download,bool start) { try { //LogStatus("Running: " + download.Guid,NotifyType.StatusMessage); // Store the download so we can pause/resume. activeDownloads.Add(download); Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress); if(start) { // Start the download and attach a progress handler. await download.StartAsync().AsTask(cts.Token,progressCallback); } else { // The download was already running when the application started, re-attach the progress handler. await download.AttachAsync().AsTask(cts.Token,progressCallback); } ResponseInformation response = download.GetResponseInformation(); // GetResponseInformation() returns null for non-HTTP transfers (e.g., FTP). string statusCode = response != null ? response.StatusCode.ToString() : String.Empty; //LogStatus( // String.Format( // CultureInfo.CurrentCulture, // "Completed: {0}, Status Code: {1}", // download.Guid, // statusCode), // NotifyType.StatusMessage); } catch(TaskCanceledException) { //LogStatus("Canceled: " + download.Guid,NotifyType.StatusMessage); } catch{ //if(!IsExceptionHandled("Execution error",ex,download)) { //throw; //} } finally { activeDownloads.Remove(download); } }
/// <summary> /// Hanbdles a single BackgroundDownload for a song. /// </summary> private async void HandleDownload(Track track, DownloadOperation download, bool start) { track.BackgroundDownload = new BackgroundDownload(download); ActiveDownloads.Add(track); Debug.WriteLine("Added {0} to active downloads", track); try { var progressCallback = new Progress<DownloadOperation>(DownloadProgress); if (start) { // Start the BackgroundDownload and attach a progress handler. await download.StartAsync() .AsTask(track.BackgroundDownload.CancellationTokenSrc.Token, progressCallback); } else { // The BackgroundDownload was already running when the application started, re-attach the progress handler. await download.AttachAsync() .AsTask(track.BackgroundDownload.CancellationTokenSrc.Token, progressCallback); } //Download Completed var response = download.GetResponseInformation(); //Make sure it is success if (response.StatusCode < 400) { await DownloadFinishedForAsync(track); } else { Debug.WriteLine("Download status code for {0} is bad :/", track); track.Status = TrackStatus.None; await _libraryService.UpdateTrackAsync(track); await ((DownloadOperation)track.BackgroundDownload.DownloadOperation).ResultFile.DeleteAsync(); } } catch { Debug.WriteLine("Download cancelled {0}", track); track.AudioLocalUri = null; track.Status = TrackStatus.None; await _libraryService.UpdateTrackAsync(track); await ((DownloadOperation)track.BackgroundDownload.DownloadOperation).ResultFile.DeleteAsync(); } finally { ActiveDownloads.Remove(track); } }
private async Task HandleDownloadAsync(DownloadOperation download) { try { // For more advanced Background Transfer features, please take a look at the // BackgroundTransfer sample. if (download.Progress.Status == BackgroundTransferStatus.Idle) { await download.StartAsync(); } else { await download.AttachAsync(); } ResponseInformation response = download.GetResponseInformation(); // GetResponseInformation() returns null for non-HTTP transfers (e.g., FTP). string statusCode = response != null ? response.StatusCode.ToString() : String.Empty; rootPage.NotifyUser(String.Format(CultureInfo.CurrentCulture, "Successfully completed background download: {0}, Status Code: {1}", download.Guid, statusCode), NotifyType.StatusMessage); } catch (Exception ex) { rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage); } }
private async Task HandleDownloadAsync(DownloadOperation download, bool start) { try { //LogStatus("Running: " + download.Guid, NotifyType.StatusMessage); // Store the download so we can pause/resume. //activeDownloads.Add(download); cts = new CancellationTokenSource(); //Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress); if (start) { // Start the download and attach a progress handler. await download.StartAsync().AsTask(cts.Token, null); } else { // The download was already running when the application started, re-attach the progress handler. await download.AttachAsync().AsTask(cts.Token, null); } ShowMessageDialog("Download Complete!"); ResponseInformation response = download.GetResponseInformation(); //LogStatus(String.Format("Completed: {0}, Status Code: {1}", download.Guid, response.StatusCode), //NotifyType.StatusMessage); } catch (TaskCanceledException e) { //LogStatus("Canceled: " + download.Guid, NotifyType.StatusMessage); } catch (Exception ex) { ShowMessageDialog("Execution error!12"); //if (!IsExceptionHandled("Execution error", ex, download)) //{ // throw; //} } finally { //activeDownloads.Remove(download); } }
private async Task HandleDownloadAsync(DownloadOperation download, bool start) { try { // Store the download so we can pause/resume. activeDownloads.Add(download); if (start) { // Start the download and attach a progress handler. await download.StartAsync(); } else { // The download was already running when the application started, re-attach the progress handler. await download.AttachAsync(); } } catch (TaskCanceledException) { } catch (Exception ex) { } finally { activeDownloads.Remove(download); } }
private async Task HandleDownloadAsync(DownloadOperation download, bool start) { try { LogStatus("Running: " + download.Guid, NotifyType.StatusMessage); // Store the download so we can pause/resume. activeDownloads.Add(download); Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress); if (start) { // Start the download and attach a progress handler. await download.StartAsync().AsTask(cts.Token, progressCallback); } else { // The download was already running when the application started, re-attach the progress handler. await download.AttachAsync().AsTask(cts.Token, progressCallback); } ResponseInformation response = download.GetResponseInformation(); LogStatus(String.Format(CultureInfo.CurrentCulture, "Completed: {0}, Status Code: {1}", download.Guid, response.StatusCode), NotifyType.StatusMessage); } catch (TaskCanceledException) { LogStatus("Canceled: " + download.Guid, NotifyType.StatusMessage); } catch (Exception ex) { if (!IsExceptionHandled("Execution error", ex, download)) { throw; } } finally { activeDownloads.Remove(download); } }
private async Task HandleDownloadAsync(DownloadOperation download, DownloadInfo info, bool start) { try { _activeDownloads[info.ChannelId] = download; _cts[info.ChannelId] = new CancellationTokenSource(); var progressCallback = new Progress<DownloadOperation>(downOperation => { DownloadProgress(downOperation, info); }); if (start) { await download.StartAsync().AsTask(_cts[info.ChannelId].Token, progressCallback); } else { await download.AttachAsync().AsTask(_cts[info.ChannelId].Token, progressCallback); } _activeDownloads.Remove(info.ChannelId); _cts.Remove(info.ChannelId); var response = download.GetResponseInformation(); _isDownloading = false; DownloadCompleted(info); } catch (System.Threading.Tasks.TaskCanceledException) { } catch (Exception ex) { _isDownloading = false; DownloadFailture(info, ex.Message); } }
private async Task HandleDownloadAsync(DownloadOperation download, bool start, IProgress<int> progress = null, CancellationToken cancelToken = default(CancellationToken)) { var app = Application.Current as App; try { // Store the download so we can pause/resume. app.activeDownloads.Add(download); Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>((operation) => { if (operation.Progress.TotalBytesToReceive != 0) { ulong val = (ulong)(operation.Progress.BytesReceived * 100 / operation.Progress.TotalBytesToReceive); progress.Report((int)val); } }); if (start) { // Start the download and attach a progress handler. await download.StartAsync().AsTask(cancelToken, progressCallback); } else { // The download was already running when the application started, re-attach the progress handler. await download.AttachAsync().AsTask(cancelToken, progressCallback); } } catch (TaskCanceledException) { } catch (Exception) { } finally { app.activeDownloads.Remove(download); } }
private void AttachDownloadOperationHandles(DownloadOperation download) { CurrentBlock.Text += String.Format( "Download {0} has status {1}.\r\n", download.Guid, download.Progress.Status); var asyncInfo = download.AttachAsync(); asyncInfo.Progress = new AsyncOperationProgressHandler<DownloadOperation, DownloadOperation>(OnAttachedDownloadProgress); asyncInfo.Completed = new AsyncOperationWithProgressCompletedHandler<DownloadOperation, DownloadOperation>(OnAttachedDownloadCompleted); }
/// <summary> /// Hanbdles a single BackgroundDownload for a song. /// </summary> /// <param name="song"> /// The song to be downloaded /// </param> /// <param name="download"> /// The download operation /// </param> /// <param name="start"> /// Either the download is started or just handled /// </param> private async void HandleDownload(Song song, DownloadOperation download, bool start) { if (song == null || download == null) { return; } song.Download = new BackgroundDownload(download); ActiveDownloads.Add(song); try { var progressCallback = new Progress<DownloadOperation>(DownloadProgress); if (start) { // Start the BackgroundDownload and attach a progress handler. await download.StartAsync().AsTask(song.Download.CancellationTokenSrc.Token, progressCallback); } else { // The BackgroundDownload was already running when the application started, re-attach the progress handler. await download.AttachAsync().AsTask(song.Download.CancellationTokenSrc.Token, progressCallback); } // Download Completed var response = download.GetResponseInformation(); // Make sure it is success if (response.StatusCode < 400) { await DownloadFinishedForAsync(song); } else { song.SongState = SongState.None; sqlService.UpdateItem(song); download.ResultFile.DeleteAsync(); } } catch { song.SongState = SongState.None; sqlService.UpdateItem(song); download.ResultFile.DeleteAsync(); } finally { ActiveDownloads.Remove(song); } }
private async Task HandleDownloadAsync(DownloadOperation download, bool start) { // Create progress callback Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress); // Create cancellation token CancellationTokenSource cts = new CancellationTokenSource(); if (start) { // Start the download and attach a progress handler. await download.StartAsync().AsTask(cts.Token, progressCallback); } else { // The download was scheduled in a previous session, re-attach the progress handler. await download.AttachAsync().AsTask(cts.Token, progressCallback); } }
private static async Task HandleDownloadAsync(DownloadOperation download, bool start, Action<DownloadOperation> downloadProgress) { CancellationTokenSource cts = new CancellationTokenSource(); DownloadCancelOperation opration = new DownloadCancelOperation() { DownloadKey=download.ResultFile.Name, DownLoad = download, CancelToken = cts }; try { Debug.WriteLine("Running: " + download.Guid); // Store the download so we can pause/resume. ActiveDownloads.Add(opration); if (downloadProgress!=null) { Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(downloadProgress); if (start) { // Start the download and attach a progress handler. await download.StartAsync().AsTask(cts.Token, progressCallback); } else { // The download was already running when the application started, re-attach the progress handler. await download.AttachAsync().AsTask(cts.Token, progressCallback); } } else { if (start) { await download.StartAsync().AsTask(cts.Token); } else { await download.AttachAsync().AsTask(cts.Token); } } ResponseInformation response = download.GetResponseInformation(); // GetResponseInformation() returns null for non-HTTP transfers (e.g., FTP). string statusCode = response != null ? response.StatusCode.ToString() : String.Empty; Debug.WriteLine( String.Format( CultureInfo.CurrentCulture, "Completed: {0}, Status Code: {1}", download.Guid, statusCode )); } catch (TaskCanceledException) { Debug.WriteLine("Canceled: " + download.Guid); } catch (Exception ex) { if (!IsExceptionHandled("Execution error", ex, download)) { throw; } } finally { opration.CancelToken.Dispose(); ActiveDownloads.Remove(opration); } }
private async Task ResumeDownloadAsync(DownloadOperation downloadOperation) { //backButton_Copy.IsEnabled = false; _activeDownload = downloadOperation; Uri uri = downloadOperation.RequestedUri; string filename = System.IO.Path.GetFileName(uri.LocalPath); downloadfilename.Text = String.Format("Downloading '{0}'", filename);//downloadOperation.ResultFile.Name); var progress = new Progress<DownloadOperation>(ProgressCallback); await downloadOperation.AttachAsync().AsTask(progress); }
private async Task HandleDownloadAsync(DownloadOperation download, bool start) { try { Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress); if (start) { await download.StartAsync().AsTask(progressCallback); } else { await download.AttachAsync().AsTask(progressCallback); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); UpdateTaskCompleted(); } }
private async void handleDownloadAsync(DownloadOperation download, Episode episode, DownloadState downloadState) { try { logger.LogMessage($"EpisodesViewModel: Registering download of file {download.ResultFile.Name}.", LoggingLevel.Information); activeDownloadsByDownload.Add(download, episode); if (activeDownloadsByEpisode.Keys.Contains(episode)) { activeDownloadsByEpisode[episode].Add(download); } else { activeDownloadsByEpisode.Add(episode, new List<DownloadOperation> { download }); } Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(downloadProgress); #if DEBUG download.CostPolicy = BackgroundTransferCostPolicy.Always; #endif if (downloadState == DownloadState.NotStarted) { logger.LogMessage($"EpisodesViewModel: Download hasn't been started yet. Starting it."); await download.StartAsync().AsTask(progressCallback).ConfigureAwait(false); } if (downloadState == DownloadState.AlreadyRunning) { logger.LogMessage($"EpisodesViewModel: Download has been already started. Attaching progress handler to it."); await download.AttachAsync().AsTask(progressCallback); } ResponseInformation response = download.GetResponseInformation(); logger.LogMessage($"EpisodesViewModel: Download of {download.ResultFile.Name} completed. Status Code: {response.StatusCode}", LoggingLevel.Information); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => episode.Status = Loaded); } catch (TaskCanceledException) { logger.LogMessage("Download cancelled."); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => episode.Status = CanBeLoaded); await fileUtils.TryDeleteFile(download.ResultFile.Name); } catch (Exception ex) { logger.LogMessage($"EpisodesViewModel: Download error. {ex.Message}", LoggingLevel.Error); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => episode.Status = CanBeLoaded); await fileUtils.TryDeleteFile(download.ResultFile.Name); } finally { unregisterDownlod(download, episode); } }
private async Task HandleDownloadAsync(DownloadOperation download, bool start, Func<IStorageFile, Task> finish, Action<string> callBack, Action<double> progressHandler) { _progressCall = progressHandler; string notifica = ""; try { Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress); if (start) { // Start the download and attach a progress handler. await download.StartAsync().AsTask(cts.Token, progressCallback); } else { // The download was already running when the application started, re-attach the progress handler. await download.AttachAsync().AsTask(cts.Token, progressCallback); } ResponseInformation response = download.GetResponseInformation(); } catch (TaskCanceledException) { notifica = "Download Cancelado, tente novamente mais tarde"; //LogStatus("Canceled: " + download.Guid, NotifyType.StatusMessage); } catch (Exception ex) { notifica = "-- Erro desconhecido --"; notifica = string.Format("{0}\n{1}", notifica, ex.ToString()); } finally { _downloadOp = null; } if (notifica == "") { await finish(download.ResultFile); } else { await download.ResultFile.DeleteAsync(); callBack(notifica); } }