检查在后台任务中启动的下载操作的进度 [英] Check progress of download operation which started in background task

查看:34
本文介绍了检查在后台任务中启动的下载操作的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,用户可以在其中下载一些文件,这些文件也可以通过例如推送通知触发的 BackgroundTask 下载.

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.

当应用程序没有运行并且我收到推送通知时,BackgroundTask 开始下载文件.如果我启动应用程序并且文件仍在下载,我希望看到它的进度.

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.

我能够获取所有下载文件BackgroundDownloader.GetCurrentDownloadsAsync().

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

如果我有 DownloadOperation 列表,我是否能够以某种方式检查每个操作的当前进度?我的意思是如果我可以将一些 IProgress 方法附加到已经运行的下载操作中.

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.

或者有没有其他方法可以检查在BackgroundTask中启动的DownloadOperation的进度?

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

更新

如果我需要查看下载文件的进度,我可以使用这种方法:

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

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

但是,如果我只有已经下载文件的 DownloadOperation 实例,我如何检查操作进度并通知视图任何进度?有具有当前进度的 Progress 属性,但没有任何进度更改事件"或类似的东西.

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.

推荐答案

如果您已经有一个 DownloadOperation 在其他地方启动(在之前的应用程序运行、后台任务中),那么您应该 附上 到它.因此,我认为,在获得下载列表后,您应该能够以类似于开始的方式附加到它们:

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

这篇关于检查在后台任务中启动的下载操作的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆