Uwp 在用户登录时执行后台任务 [英] Uwp execute backgroundtask at user login

查看:22
本文介绍了Uwp 在用户登录时执行后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 win10 uwp,我的目标是在用户登录时执行操作,例如更新磁贴.

我查看了 msdn,似乎我需要一个 BackgroundTask.因此,我按照说明创建并注册了在此处找到的后台任务 https://msdn.microsoft.com/en-us/library/windows/apps/mt299100.aspx 和相关页面.

BackgroundTaskBuilder taskBuilder= new BackgroundTaskBuilder();taskBuilder.Name = "TaskClassName";taskBuilder.TaskEntryPoint = "TaskNameSpace.TaskClassName";//设置条件(当用户出现时执行)taskBuilder.SetTrigger(new SystemTrigger(SystemTriggerType.SessionConnected, false));taskBuilder.SetTrigger(new SystemTrigger(SystemTriggerType.UserPresent, false));var注册= taskBuilder.Register();

我的BackgroundTask 的命名空间和类放在同一个解决方案下的一个单独的项目中,并添加到 Package.appxmanifest 的声明选项卡中,并选择了属性 System Event.>

我的 IBackgroundTask 实现是

 public void Run(IBackgroundTaskInstance taskInstance){BackgroundTaskDeferral deferral = taskInstance.GetDeferral();UpdateMyTile();延迟.完成();}

在使用 Visual Studio 进行调试时,代码会正确运行并更新磁贴,所以我认为我的问题在于我如何注册任务而不是任务实现.

为了确保我已经向现有项目添加了第二个 BackgroundTask,它每 15 分钟在计时器基础上调用一次,并执行相同的功能.在第二种情况下,后台任务已注册并按预期执行.

BackgroundTaskBuilder taskBuilderT= new BackgroundTaskBuilder();taskBuilderT.Name = "TaskClassNameTimer";taskBuilderT.TaskEntryPoint = "TaskNameSpace.TaskClassNameTimer";taskBuilderT.SetTrigger(new TimeTrigger(15, false));var注册= taskBuilderT.Register();

我也试过只使用一个 SystemTrigger,首先使用 UserPresent,然后使用 SessionConnected 没有成功.

27/01/2016 更新

@Jakie:不,我没有使用 BackgroundExecutionManager.RequestAccessAsync(); 因为在 https://msdn.microsoft.com/EN-US/library/windows/apps/windows.applicationmodel.background.systemtriggertype.aspx 对于 SessionConnected 和 UserPresent 说

<块引用>

Windows 10、Windows Server 2016 Technical Preview 和 Windows 10 Mobile:您无需将应用放置在锁定屏幕上,应用就可以使用此触发器类型成功注册后台任务.

我要试试看这是否会改变当前的行为.我添加了对 BackgroundExecutionManager.RequestAccessAsync(); 的调用,使用 Windows 10 没有显示任何对话框,但它返回了 AllowedMayUseActiveRealTimeConnectivity 但在重新启动后登录时未调用任务.

我已经启动了诊断日志并找到了与我的后台任务相关的事件,它已正确注册但随后被取消.事件报告说eventID 19 Task 100.我不知道在哪里搜索这些代码的含义的信息,有什么建议吗?

解决方案

我终于在用户登录时成功更新了一个磁贴.除了我的问题中已经描述的操作之外,我已经将任务注册的方法移到了声明后台任务类的同一个项目中.

我创建了一个包含名为BackgroundRegistration 的注册方法的类,该类需要密封.我已将注册方法声明为 static 但这不是绝对必要的.

仅当在IBackgroundTask run 方法实现中调用的所有方法都包含在具有后台任务的项目中并且使用的类被密封时,我也成功地在启动用户登录时运行我的后台任务.在所有其他情况下,我发现我的后台任务总是返回错误 0x80010008,在 MS-ERREF 中被描述为:

<块引用>

调用者(客户端)消失,而被调用者(服务器)处理呼叫.

I'm learning win10 uwp and my goal is to perform an operation, like updating a tile, when the user logs in.

I've checked on msdn and it seems that a BackgroundTask is what i need. So I followed the instructions to create and register a background task found here https://msdn.microsoft.com/en-us/library/windows/apps/mt299100.aspx and on related pages.

BackgroundTaskBuilder taskBuilder= new BackgroundTaskBuilder();
taskBuilder.Name = "TaskClassName";
taskBuilder.TaskEntryPoint = "TaskNameSpace.TaskClassName";

// set conditions (execute when user become present)
taskBuilder.SetTrigger(new SystemTrigger(SystemTriggerType.SessionConnected, false));
taskBuilder.SetTrigger(new SystemTrigger(SystemTriggerType.UserPresent, false));

   var registration= taskBuilder.Register();

Namespace and class of my BackgroundTask are placed in a separated project under the same solution and added in Declaration tab of Package.appxmanifest with property System Event selected.

My IBackgroundTask implementation is

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

        UpdateMyTile();

        deferral.Complete();
    }

When debugging with visual studio the code run correclty and update the tiles so I think my issue is on how I registered the task and not in the task implementation.

To be sure I've added a second BackgroundTask to existing project which is called on timer basis every 15 minutes and that executes the same functions. In this second case the background task is registered and executes as expected.

BackgroundTaskBuilder taskBuilderT= new BackgroundTaskBuilder();
taskBuilderT.Name = "TaskClassNameTimer";
taskBuilderT.TaskEntryPoint = "TaskNameSpace.TaskClassNameTimer";

taskBuilderT.SetTrigger(new TimeTrigger(15, false));

var registration= taskBuilderT.Register();

I've also tried using only one SystemTrigger first with UserPresent and then with SessionConnected without success.

Update 27/01/2016

@Jakie: no I didn't use BackgroundExecutionManager.RequestAccessAsync(); because on https://msdn.microsoft.com/EN-US/library/windows/apps/windows.applicationmodel.background.systemtriggertype.aspx for SessionConnected and UserPresent says

Windows 10, Windows Server 2016 Technical Preview, and Windows 10 Mobile: You do not need to place an app on the lock screen before the app can successfully register background tasks using this trigger type.

I'm going to try it to see if this changes current behaviour. I've added call to BackgroundExecutionManager.RequestAccessAsync();, using windows 10 no dialog has been displayed but it has returned AllowedMayUseActiveRealTimeConnectivity but task was not called at login after a restart.

I've started diagnostic log and found the events relative to my background task, it's properly registered but then it gets canceled. Event reports says eventID 19 Task 100. I don't know where to search information on the meaning of these codes, any suggestion?

解决方案

I finally managed to updated succesfully a tile on user login. In addition to operations already described in my question I've moved the method for task registration to the same project in which background task class is declared.

I've created a class containing a registration method called BackgroundRegistration, the class needs to be sealed. I've declared the registration method as static but this is not strictly necessary.

I've also had success in running my background task at startup user login only when all methods called inside IBackgroundTask run method implementation were included in project with background task and classes used were sealed. In all other cases I found that my background task was always returning error 0x80010008 which in MS-ERREF is described as:

The caller (client) disappeared while the callee (server) was processing a call.

这篇关于Uwp 在用户登录时执行后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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