我想弄清楚爲什麼backgroundtask不會啓動,但我不知道我做錯了什麼。後臺任務不啓動
我在做什麼:我想要一個自動後臺任務,它將通過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擴展與任務「計時器」,並以正確的入口點。
所有代碼在1個項目中。
即使連接(調試程序不啓動應用程序,以及力來觸發任務)的調試器,它不會打我的任務(或斷點),並在事件查看器它給出的結果是:
帶入口點Tasks.UpdateItemTask和名稱TimeTriggeredTask的後臺任務未能激活,錯誤代碼爲0x80010008。
我一直在檢查The samples of MS background Tasks,但即使這些都沒有幫助。我會建議這並不難,但我無法得到它的工作。
也許只是在你的任務中的日誌消息,看看它是否找不到它。 – Dreamwalker 2013-02-20 08:14:50
調試器運行時沒有問題,只有它不會在任務開始時觸發斷點,這意味着任務甚至沒有被解僱......我想說,登錄任務將不會有所幫助,因爲任務根本不執行... – 2013-02-20 09:07:47
我剛剛閱讀了關於維護觸發器類別的[文檔](http://www.microsoft.com/en-us/download/details.aspx?id=27411) 。這個觸發器實際上是一個SystemTrigger而不是一個TimeTrigger(在我的appmanifest中定義)。我改變了這一點,但仍然沒有結果。我仍然有相同的錯誤信息... – 2013-02-20 10:11:34