locked
WinRT Background Task with Time Trigger Error

    Question

  • I have defined my Background Task 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 Task Register 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 triiger 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 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, & 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 Background Task., For first run, Everything runs smoothly, & for subsequent run, the compiler after reaching myRepository.Save(entity);, jumps directly to OnComplete method. WHY.???

    Tuesday, February 17, 2015 12:35 PM

Answers

  • Hi Gk_999,

    You have a method myRepository.Save(entity); I would like to know if you have too much data need to save into the database, as I know there might be some limitation on background task Supporting your app with background tasks.

    However I would like to get a sample from you instead of the code snippet, it could be quite difficult to debug.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 18, 2015 2:40 AM
    Moderator