0

I have defined my BackgroundTask as follows:

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

And TaskRegister class as:

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

}

And I have registered the trigger in my app as:

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

I also have an OnComplete method in my app:

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

The background tasks triggers successfully, and the data is saved to SQLite DB myRepository.Save(entity); table only for the first run. I came to this conclusion as I had logged the entries in OnComplete method.

The logger results confused me more.

Logger results:

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

The data in SQLite is saved only for the first Background Task "for a day". I tried to debug the BackgroundTask. The first run everything runs smoothly, but in the subsequent run the compiler jumps directly to the OnComplete method after reaching myRepository.Save(entity);. Why is it doing this?

Bono
  • 4,757
  • 6
  • 48
  • 77
Gk_999
  • 508
  • 8
  • 29

1 Answers1

0

Your background task might be killed by the system if it is using too much resources. Lock screen application have only 2 seconds of CPU time every 15 minutes. You can get more details here : https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977056.aspx

You can also get the reason about why your background task is suspended using the CheckResult() methods from BackgroundTaskCompletedEventArgs.

In your task completed event handler you can do the following:

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

It should at least given you some insight of what is going wrong.

Vincent
  • 3,656
  • 1
  • 23
  • 32