0

我将我的定义BackgroundTask如下:

public sealed class BackgroundSynchronization:IBackgroundTask
{
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();

            //code
            //Network Calls
            //Updating Local DB (SQLite)

            myRepository.Save(entity);   // Saves entity in SQLite Table, and Entity has a  Current DateTime field too.

            deferral.Complete();
        }
}

TaskRegister类:

public static class BackgroundTaskRegister {

    public static  BackgroundTaskRegistration RegisterBackgroundTask(string taskEntryPoint,string taskName,IBackgroundTrigger trigger,IBackgroundCondition condition)
    {
        foreach (var cur in BackgroundTaskRegistration.AllTasks)
        {

            if (cur.Value.Name == taskName)
            {
                return (BackgroundTaskRegistration)(cur.Value);
            }
        }

        var builder = new BackgroundTaskBuilder();

        builder.Name = taskName;
        builder.TaskEntryPoint = taskEntryPoint;
        builder.SetTrigger(trigger);

        if (condition != null)
        {
            builder.AddCondition(condition);
        }

        BackgroundTaskRegistration task = builder.Register();

        return task;
    }

}

我已在我的应用程序中将触发器注册为:

    TimeTrigger myTrigger = new TimeTrigger(15, false);
    await BackgroundExecutionManager.RequestAccessAsync();

    string entryPoint = "BackgroundTask.BackgroundSynchronization";
    string taskName = "Example per 15 minute background task";
    BackgroundTaskRegistration tsk = BackgroundTaskRegister.RegisterBackgroundTask(entryPoint, taskName, myTrigger, null);

    tsk.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);

我的应用程序中也有一个OnComplete方法:

 private async void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
 {
            var settings = ApplicationData.Current.LocalSettings;
            var message = settings.Values["backgroundSyncStatus"].ToString();
            var folder = ApplicationData.Current.LocalFolder;
            StorageFile file;
            var x = await File.DoesFileExistInLocalAsync("BackgroundTaskSucceedLog.txt");

            if (!x)
                file = await folder.CreateFileAsync("BackgroundTaskSucceedLog.txt");
            else
               file = await ApplicationData.Current.LocalFolder.GetFileAsync("BackgroundTaskSucceedLog.txt");

            await Windows.Storage.FileIO.AppendTextAsync(file, message.ToString() + " " + DateTime.Now.ToString() + Environment.NewLine);
 }

后台任务触发成功,数据myRepository.Save(entity);仅在首次运行时保存到 SQLite DB 表中。当我在OnComplete方法中记录了条目时,我得出了这个结论。

记录器的结果让我更加困惑。

记录器结果:

2/16/2015 10:51:41 PM     <-- Data Saved in SQLite for this entry
2/16/2015 11:07:03 PM
2/16/2015 11:22:26 PM
2/16/2015 11:37:39 PM
2/16/2015 11:52:51 PM
2/17/2015 5:33:00 AM       <-- Data Saved in SQLite for this entry

SQLite 中的数据仅保存“一天”的第一个后台任务。我试图调试BackgroundTask. 第一次运行一切顺利,但在随后的运行中,编译器OnComplete在到达后直接跳转到方法myRepository.Save(entity);。为什么要这样做?

4

1 回答 1

0

如果您的后台任务使用过多的资源,它可能会被系统杀死。锁屏应用程序每 15 分钟只有 2 秒的 CPU 时间。您可以在此处获取更多详细信息:https ://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977056.aspx

您还可以使用 BackgroundTaskCompletedEventArgs 中的CheckResult()方法获取有关您的后台任务暂停的原因。

在您的任务完成事件处理程序中,您可以执行以下操作:

private async void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
{
    try
    {
        args.CheckResult();
    }
    catch(Exception e)
    {
        // add a log/breakpoint here
    }
}

它至少应该让您对出了什么问题有了一些了解。

于 2015-02-20T13:12:30.600 回答