后台任务未启动 [英] Background Task doesn't start

查看:35
本文介绍了后台任务未启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚为什么后台任务无法启动,但我不知道我做错了什么.

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

我想要做什么:我想要一个自动化的后台任务,它将通过 WebApi 下载 5 个最新项目(数据是几个 kB).下载后会检查本地文件,看是否有新的项目可用.如果是这样,我想在 LiveTile 上创建一个包含新项目数量的徽章.

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.

我有以下代码:

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;
}

我的工作是这样的:

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();
        }
    }
}

在我的 appmanifest 中,backgroundTask 扩展了任务计时器"和正确的入口点.

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

所有代码都在 1 个项目中.

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:

具有入口点 Tasks.UpdateItemTask 和名称 TimeTriggeredTask 的后台任务无法激活,错误代码为 0x80010008.

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

我一直在检查 MS 背景样本任务,但即使是那些也无济于事.我建议这并不难,但我无法让它工作.

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.

推荐答案

我终于搞定了!!

首先:我已将任务作为 Windows RT 组件类型移动到新程序集中,并在我的 WinRT 应用程序中添加了一个引用.

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

第二:我在 1 个 cs 文件中有 BackgroundTaskRegistration 和 UpdateItemTask,只有任务是密封的.我需要将 BackgroundTaskRegistration 也设置为密封,以便编译.一旦我强制触发触发器,它最终会到达断点...

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...

这篇关于后台任务未启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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