Search code examples
c#windows-phone-8.1background-task

BackgroundTask Clarification


First, let me say that I have done my research. I have spent about a day and a half watching videos, reading MSDNs, testing, and experimenting. I have yet to solve this problem. Despite all my research, this is going to be fairly broad question, so I apologize for that. This is probably partially in part due to the newness of Windows Phone 8.1.

My goal is to have an int increment once per hour, even when the app is not running. Clearly, the best way to do this is via a TimeTrigger and BackgroundTask. I have created my separate project (Windows Runtime Component) called BackgroundTaskProject in my solution. I renamed its class to BackgroundTaskClass. I referenced BackgroundTaskProject from my main project.

In BackgroundTaskClass, I implemented IBackgroundTask and its Run method. Question 1: If all I am trying to do is increment a value in my main project, does anything go in this method here? If so, what?

I have copy and pasted my RegisterBackgroundTask method from MSDN into BackgroundTaskClass.

In my main project, I created my TimeTrigger. I registered my BackgroundTask via the static RegisterBackgroundTask method in BackgroundTaskClass. I made changes so that this BackgroundTask is assigned to a variable of type Task<BackgroundTaskRegistration> due to the await/async methods.

The ultimate question: Where do I say myInt++ to have it run once per hour? (myInt is in my main project.)

I can provide specific code or answer questions as requested, but here is some that you may need:

In main project/class:

TimeTrigger hourlyTrigger = new TimeTrigger(60, false);
Task<BackgroundTaskRegistration> task = BackgroundTaskProject.BackgroundTaskClass.RegisterBackgroundTask("BackgroundTaskProject.BackgroundTaskClass", "Number Incrementer", hourlyTrigger, null);

In BackgroundTaskClass:

Copy and pasted from MSDN (http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977055.aspx and http://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj553413.aspx)

Solution

  • I am not very well versed in Windows 8 phone, but I can see you have a basic misconception: Your myInt goes nowhere.

    The problem is you are assuming something will be kept always running, but this is not so.

    Your main application may be suspended at any time, at this point you should save any data you need to resume the application -> i.e. once suspended your app is gone and it is no longer in memory so there is no myInt to increment.

    Your background task fares even worse: it is removed from memory as soon as it finishes execution of the run method -> i.e. if you increment a myInt in your background task then the next time your background task is triggered it will be set to its initial value, because it is a new background task.

    The correct way to do this is: when your app is started for the first time save the time and intial value of myInt into the app storage. Now whenever you need to figure out the value of myInt simply calculate on the fly from those initial values.