Check progress of download operation which started in background task

送分小仙女□ 提交于 2019-12-23 23:09:58

问题


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.


回答1:


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



回答2:


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
    }


来源:https://stackoverflow.com/questions/34795800/check-progress-of-download-operation-which-started-in-background-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!