private async void OnRangesDownloaded(DownloadOperation sender, BackgroundTransferRangesDownloadedEventArgs args) { // BackgroundTransfer will wait for the app's RangesDownloaded event handlers to finish executing before // triggering the next RangesDownloaded event. Any download progress made while the app's handlers are // executing get coalesced into a single RangesDownloaded event. // // When calling async APIs inside this handler, we must first take a deferral and then complete it once we // are done processing the event. Otherwise, BackgroundTransfer will assume that the handler is done executing // as soon as the synchronous portion of the handler returns, even if there are still tasks running. using (args.GetDeferral()) { // AddedRanges tells you which ranges were downloaded since the last RangesDownloaded event. // To get the full list of downloaded ranges, use the GetDownloadedRanges method. string message = " Newly-downloaded ranges: " + FormatDownloadedRanges(args.AddedRanges); // A download may restart if the server contents changed while the download is in progress. if (args.WasDownloadRestarted) { message += " (download was restarted)"; } // Wait for the UI update to happen before completing the deferral. That way, we can be certain that // the next RangesDownloaded event will not get triggered midway through the UI update. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { DownloadedRangesText.Text = FormatDownloadedRanges(sender.GetDownloadedRanges()); rootPage.NotifyUser(message, NotifyType.StatusMessage); }); } }