2

I have an application where users can download some files and these files can be also downloaded via BackgroundTask triggered by push notification for example.

When the app is not running and I receive push notification, BackgroundTask starts to download the file. If I launch the app and the file is still downloading I would like to see progress of it.

I am able to get all downloading files BackgroundDownloader.GetCurrentDownloadsAsync().

If I have list of DownloadOperation, am I able to somehow check current progress of each operation? I mean if I can attach some IProgress<T> method to already running download operations.

Or is there any other approach how to check progress of DownloadOperation which started in BackgroundTask?


UPDATE

If I need to see progress of downloading file I can use this approach:

await downloadOperation.StartAsync().AsTask(token, new Progress<DownloadOperation>(...));

However, if I have just instance of DownloadOperation which is already downloading a file, How can I check progress of the operation and inform view about any progress? There is Progress property which has current progress, but there is not any "progress changed event" or something like that.

Jakub Krampl
  • 1,774
  • 1
  • 16
  • 24
  • You mean any other method than attaching with *IProgress*? Have you checked [DownloadOperation.Progress](https://msdn.microsoft.com/en-US/library/windows/apps/windows.networking.backgroundtransfer.downloadoperation.progress.aspx) property? – Romasz Jan 14 '16 at 18:20
  • @Romasz I would appreciate any solution. It can be simple and I just do not see it. Do you mean to use some kind of timer and check `Progress` property or what exactly? – Jakub Krampl Jan 14 '16 at 21:35
  • I must say I don't understand something - as you have said, you are able to get *DownloadOperations* with `var downloads = await BackgroundDownloader.GetCurrentDownloadsAsync();` <- it's *IReadOnlyList* with download operations - have you tried to check for each item (or desired one) its *Progress* property? – Romasz Jan 14 '16 at 21:39
  • @Romasz I have updated my question. – Jakub Krampl Jan 15 '16 at 09:08

2 Answers2

1

Handle ProgressChanged event of Progress<T> class.

  var progress = new Progress<DownloadOperation>();
      progress.ProgressChanged += ProgressOnProgressChanged;

 private void ProgressOnProgressChanged(object sender, string s)
    {
      // your logic here
    }
Radin Gospodinov
  • 2,313
  • 13
  • 14
0

If you already have a DownloadOperation which was started elsewhere (in previous app run, background task) then you should just Attach to it. So as I think, after getting list of downloads, you should be able to attach to them, similar way like you have started:

var downloads = await BackgroundDownloader.GetCurrentDownloadsAsync();

foreach(var operation in downloads)
    await operation.AttachAsync().AsTask(token, new Progress<DownloadOperation>(...)); // of course for each operation there will be some changes
Romasz
  • 29,662
  • 13
  • 79
  • 154