Windows8 BackgroundTransfer DownLoad
private List<DownloadOperation> activeDownloads; private CancellationTokenSource cts; public ScenarioInput1() { InitializeComponent(); cts = new CancellationTokenSource(); } async void FrameLoaded(object sender, object e) { await DiscoverActiveDownloadsAsync(); } private async Task DiscoverActiveDownloadsAsync() { activeDownloads = new List<DownloadOperation>(); IReadOnlyList<DownloadOperation> downloads = null; try { downloads = await BackgroundDownloader.GetCurrentDownloadsAsync(); if (downloads.Count > 0) { List<Task> tasks = new List<Task>(); foreach (DownloadOperation download in downloads) { tasks.Add(HandleDownloadAsync(download, false)); } await Task.WhenAll(tasks); } } catch (Exception ex){ return; } } private async Task HandleDownloadAsync(DownloadOperation download, bool isNewDownload) { try { // Store the download so that we can pause/resume. activeDownloads.Add(download); Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress); if (isNewDownload) { // 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) { } catch (Exception ex){ } finally { activeDownloads.Remove(download); } } private void DownloadProgress(DownloadOperation download) { double percent = 100; if (download.Progress.TotalBytesToReceive > 0) percent = download.Progress.BytesReceived * 100 / download.Progress.TotalBytesToReceive; if (download.Progress.HasRestarted) Debug.WriteLine(" - Download Restarted"); } private async void StartDownload_Click(object sender, RoutedEventArgs e) { Uri sourceURI; if (!Uri.TryCreate(this.serverAddressField.Text.Trim(), UriKind.Absolute, out sourceURI)) return; string destination = this.fileNameField.Text.Trim(); StorageFile destinationFile; try { destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(destination, CreationCollisionOption.GenerateUniqueName); } catch (FileNotFoundException ex) { return; } BackgroundDownloader downloader = new BackgroundDownloader(); DownloadOperation download = downloader.CreateDownload(sourceURI, destinationFile); await HandleDownloadAsync(download, true); } private void PauseAll_Click(object sender, RoutedEventArgs e) { foreach (DownloadOperation download in activeDownloads) { if (download.Progress.Status == BackgroundTransferStatus.Running) download.Pause(); } } private void ResumeAll_Click(object sender, RoutedEventArgs e) { foreach (DownloadOperation download in activeDownloads) { if (download.Progress.Status == BackgroundTransferStatus.PausedByApplication) download.Resume(); } } private void CancelAll_Click(object sender, RoutedEventArgs e) { cts.Cancel(); cts.Dispose(); // Re-create the CancellationTokenSource and activeDownloads for future downloads. cts = new CancellationTokenSource(); activeDownloads = new List<DownloadOperation>(); }
posted on 2013-03-28 14:56 JackSlaterYu 阅读(382) 评论(0) 编辑 收藏 举报