在 UWP 中无限期运行后台任务 [英] Run Background Task Indefinitely in UWP

查看:63
本文介绍了在 UWP 中无限期运行后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要不断轮询 API 并在 UWP 应用中提供通知,无论它是对用户可见还是最小化未聚焦暂停.

I have a requirement to constantly poll an API and provide notifications in UWP app irrespective of whether it is Visible to user OR minimized OR Not in focus OR Suspended.

如果我选择在应用程序本身中编写它,它会在应用程序暂停时停止工作,并在应用程序再次打开或保持焦点时恢复.所以我选择了后台任务,这样它即使在应用程序暂停时也能运行(例如:最小化).

If I choose to write it in the app itself, it stopped working while app is suspended and resumed while again app is opened or kept in focus. So I chosen Background Task so that it runs everytime even when app is suspended (ex: minimized).

但现在任何后台任务都被限制在 30 秒的短跨度内,如果它是使用应用程序触发器触发的,则最多持续 10 分钟.

But now any background task is limited to short span of 30secs and if it is triggered using Application Trigger it lasts max for 10min.

我尝试在 10 分钟后重新触发后台任务,但在重新触发阶段,如果应用程序最小化或设备被锁定,则不会发生.仅在我恢复失去无限期行为的应用程序后,才会发生重新触发.

I tried re triggering the background task on completion of 10mins, but during the re trigger phase if app is minimized or device is locked it doesn't happen. Re trigger is happening only after I resume the app by which I'm loosing the indefinite behavior.

我还尝试添加受限功能以无限期地运行任务,但这不适用于 Store 应用程序,仅适用于侧加载.但我的要求是支持 Store 应用程序.

I also tried adding the restricted capability to run task indefinitely but this is not allowed for Store app and only for Side loading. But My requirement is to support this for Store app.

请帮助我如何使用后台任务或 UWP 平台中可用的任何其他模型来实现此行为.

Kindly help me how to achieve this behavior using Background Task, or else any other model available in UWP platform to achieve this.

推荐答案

要长时间/无限期地运行后台任务,您需要在应用的清单中声明 extendedBackgroundTaskTime 功能.

To run background tasks for extended/indefinite periods of time you need to declare the extendedBackgroundTaskTime capability in your app's manifest.

https://docs.microsoft.com/en-us/windows/uwp/launch-resume/run-in-the-background-indefinetly

这篇关于在 UWP 中无限期运行后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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