I am developing a Windows Phone 8 app that would have a live flip tile. I will be creating a scheduled agent (periodic task not resource intensive) which will update the live tile using local data.
The whole app does not connect to the internet in any way, it uses only local data. So push notifications are out of the question.
I need to update the live tile from the background agent and/or from the foreground app when it's launched whichever happens first.
However how can I ensure the foreground app and the background agent do not step on each other's toes? I have two main options to do this :<ul><li>
Use a mutex (AFAIK the background agent runs in a different process so locks (monitor that is) is out of the question). But I'm afraid it would have a high performance cost (obtaining and releasing the mutex, that is).</li> <li>
When the foreground app starts I would always remove the agent, do its work and reschedule the agent back. Theferore there would not be any chance of overlapping between the foreground app and the background agent, BUT the whole add/remove agent could also be lengthy and, furthemore, the user might close the app after this removed the agent but before it added it back.</li> </ul>
I am really torn between these two approaches and can't tell which would be best.
PS : You can't reschedule an agent from the agent's own code, right?Answer1:
The mutex option is <a href="http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202944%28v=vs.105%29.aspx#BKMK_CommunicationBetweenForegroundApplicationandBackgroundAgent" rel="nofollow">officially recommended by the MSDN documentation</a> for both Windows Phone 7 and 8.
I have used this option and experienced no noticeable performance problems when sharing data between the Background Agent and Foreground App, and I target WP7.1 and test on 1st and 2nd generation WP7 devices (LG E900 and Lumia 800).
Obviously, when using mutex, the key is to lock the resource for as short a time as possible. And when trying to acquire a lock on a resource in a Background Agent, the timeout feature of
WaitHandle.WaitOne is very useful as the Background Agent only has 25 seconds to run.
<em>NOTE:</em> If you are targeting Windows Phone 8 only, or via a 2nd project/binary then a different approach (named events for inter-process-communication) is available. <a href="http://blogs.windows.com/windows_phone/b/wpdev/archive/2013/03/27/using-named-events-to-coordinate-foreground-apps-and-background-agents.aspx" rel="nofollow">Peter Torr wrote about it on the official Windows Phone Developer blog</a>.
<br /><strong>Enabled/Disable Background Agent</strong>
The second approach you mention is riskier because the we don't know what guarantees the OS gives for adding or removing of the Background Agent - what happens if removal if not instant? what happens if a newly renewed Background Agent fires while to Foreground App is running? etc.
<br /><strong>Agent Renewal</strong>
Background Agents cannot renew themselves - once added, <a href="http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202942%28v=vs.105%29.aspx#BKMK_ConstraintsforallScheduledTaskTypes" rel="nofollow">they will run for a maximum of two weeks</a> unless renewed by the Foreground App. This will prevent Background Agents running for apps the user no longer uses.