Background Task doesn't start

3

I trying to figure out why the backgroundtask won't start, but I have no idea, what I'm doing wrong.

What am I trying to do: I want to have an automated background task which will download the 5 latest items through a WebApi (data is a couple kB). After downloading it will check the local file, to see if there are any new items available. If so, I want to create a Badge on the LiveTile with the number of new items.

I have the following code:

private BackgroundTaskRegistration ScheduleBackgroundTask()
{
    foreach (var cur in BackgroundTaskRegistration.AllTasks)
    {
        if (cur.Value.Name == "TimeTriggeredTask")
        {
            return (BackgroundTaskRegistration)(cur.Value);
        }
    }

    var builder = new BackgroundTaskBuilder();

    builder.Name = "TimeTriggeredTask";
    builder.TaskEntryPoint = "Tasks.UpdateItemTask";
    builder.SetTrigger(new MaintenanceTrigger(15, false));
    builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
    builder.AddCondition(new SystemCondition(SystemConditionType.UserNotPresent));

    BackgroundTaskRegistration task = builder.Register();
    return task;
}

And my job looks like this:

namespace Tasks
{
    public sealed class UpdateItemTask : IBackgroundTask
    {
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            Debug.WriteLine("Starting");

            BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();

            DataHandler dataHandler = new DataHandler();
            BindableCollection<GeekAndPokeItemViewModel> latestItemsOnline = await dataHandler.GetData("10");
            BindableCollection<GeekAndPokeItemViewModel> latestItemsLocal = await dataHandler.GetLocalData();

            int difference = latestItemsOnline.Except(latestItemsLocal).Count();

            if (difference > 0)
            {
                BadgeUpdateManager.CreateBadgeUpdaterForApplication().Clear();
                BadgeNumericNotificationContent badgeContent = new BadgeNumericNotificationContent((uint)difference);

                // send the notification to the app's application tile
                BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeContent.CreateNotification());
            }

            _deferral.Complete();
        }
    }
}

In my appmanifest the backgroundTask is extended with the Task "timer" and with the correct entrypoint.

All code is in 1 project.

Even with the debugger attached (debug the program without starting the app, and force to fire the task), it wont hit my task (or breakpoint) and in the eventviewer it gives the result of:

The background task with entry point Tasks.UpdateItemTask and name TimeTriggeredTask failed to activate with error code 0x80010008.

I've been checking The samples of MS background Tasks, but even those are not helping. I would suggest this isn't that hard but I cant get it work.

c#
windows-runtime
asked on Stack Overflow Feb 20, 2013 by pazcal • edited Feb 20, 2013 by abatishchev

1 Answer

5

I finally got it working!!

First: I've moved the Tasks into a new assembly as a Windows RT Component Type and added a reference in my WinRT app.

Second: I had the BackgroundTaskRegistration and the UpdateItemTask in 1 cs file, with only the task as sealed. I needed to set the BackgroundTaskRegistration also as sealed, in order to compile. Once I force to fire the trigger it finally hits the breakpoint...

answered on Stack Overflow Mar 5, 2013 by pazcal

User contributions licensed under CC BY-SA 3.0