UWP在用户登录时执行backgroundtask [英] Uwp execute backgroundtask at user login

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

问题描述

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

我检查了msdn,看来BackgroundTask是我所需要的.因此,我按照说明在此处创建并注册了后台任务 https://msdn.microsoft.com/zh-CN/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 registration = taskBuilder.Register(); 

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

我的 IBackgroundTask 实现是

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

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

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

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

我还尝试过先仅将一个 SystemTrigger UserPresent 一起使用,然后再与 SessionConnected 一起使用,但均未成功.

更新27/01/2016

@Jakie:不,我没有使用 BackgroundExecutionManager.RequestAccessAsync(); ,因为在解决方案

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

我创建了一个包含名为 BackgroundRegistration 的注册方法的类,该类需要进行 Sealed 密封.我已将注册方法声明为 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在用户登录时执行backgroundtask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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