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.