后台任务无法启动

时间:2013-02-20 08:05:43

标签: c# windows-runtime

我试图找出为什么后台任务无法启动,但我不知道,我做错了什么。

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

我有以下代码:

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使用Task“timer”和正确的入口点进行扩展。

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

即使连接了调试器(在不启动应用程序的情况下调试程序,并强制启动任务),它也不会触及我的任务(或断点),在事件查看器中它会得到以下结果:

  

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

我一直在检查The samples of MS background Tasks,但即便是那些也无济于事。我建议这不是那么难,但我不能让它发挥作用。

1 个答案:

答案 0 :(得分:5)

我终于开始工作了!!

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

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

相关问题