简体   繁体   English

如何查看基于JavaScript的UWP是否在“后台任务”上下文中运行?

[英]How can I see if my JavaScript-based UWP is running in a “background task” context?

UWP supports "background tasks" , which run in the background, for example in response to some system event or to implement an App Service . UWP支持“后台任务” ,该后台任务在后台运行,例如,响应某些系统事件实现App Service

JavaScript-based UWP apps declare background tasks in their respective application manifest files ( .appxmanifest ). 基于JavaScript的UWP应用在其各自的应用清单文件( .appxmanifest )中声明后台任务

What is the best practice to detect whether my code is running as a background task? 检测我的代码是否作为后台任务运行的最佳实践是什么?

I assume if you are asking this question, you have built, registered, and executed a background task, which can only be done from the UI thread using the ApplicationTrigger ( https://docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.background.applicationtrigger ). 我假设如果您要问这个问题,那么您已经建立,注册并执行了后台任务,该任务只能使用ApplicationTrigger( https://docs.microsoft.com/zh-cn/uwp/ api / windows.applicationmodel.background.applicationtrigger )。 If you have not, then your app may be in prelaunch ( https://docs.microsoft.com/en-us/windows/uwp/launch-resume/handle-app-prelaunch ). 如果您还没有,则您的应用程序可能正在预启动( https://docs.microsoft.com/zh-cn/windows/uwp/launch-resume/handle-app-prelaunch )。 Otherwise, you should be able to handle reporting that yourself programmatically before and after you call requestAsync() or track progress of your background task through reporting within your background task via Windows.UI.WebUI.WebUIBackgroundTaskInstance.Progress 否则,您应该能够在调用requestAsync()之前和之后以编程方式处理自己的报告,或者通过Windows.UI.WebUI.WebUIBackgroundTaskInstance.Progress通过后台任务内的报告来跟踪后台任务的进度

As per guidance, first you need to Create a background task class and register it to run when your app is not in the foreground. 按照指导,首先您需要创建一个后台任务类,并注册它以在您的应用程序不在前台时运行。

Background tasks are decoupled from the app, and they run separately, but background task progress and completion can be monitored by app code. 后台任务与应用程序分离,并且它们分别运行,但是可以通过应用程序代码监视后台任务的进度和完成情况。 To make this happen, the app subscribes to events from the background task(s) it has registered with the system. 为实现此目的,该应用程序订阅了已在系统中注册的后台任务中的事件。 You can use the BackgroundTaskProgressEventHandler 您可以使用BackgroundTaskProgressEventHandler

var backgroundTaskProgressEventHandler = function(sender, args) {
 /* Your code */
}

UWP "background tasks" are implemented as modified web workers. UWP“后台任务”被实现为修改后的Web Worker。 Detecting web worker context is covered in some other SO questions ( 1 , 2 ). 检测网络工作者上下文被覆盖在一些其他SO问题( 12 )。 One solution is the following: 一种解决方案如下:

function inBackgroundTaskContext() {
    return typeof importScripts === 'function'
}

importScripts is a function that is callable from within a web worker context. importScripts是可从Web Worker上下文中调用的函数。 It is not defined when the app is running within a "normal" app context. 当应用程序在“正常”应用程序上下文中运行时,未定义它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM