83747

Periodic task concurrency issue with foreground app

Question:

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:

<strong>Mutex</strong>

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.

Recommend

  • jQuery - Simple “on('click')” Selector Issue
  • How should applciation using active/passive redundant model be containerized using kubernetes?
  • Play! framework: can I reschedule the job?
  • How locks (S,X,IS,IX) work in Mysql with queries like FOR UPDATE/LOCK IN SHARE MODE?
  • How to reschedule a job in android in case of failure using job scheduler
  • Event handler not working in Internet Explorer
  • pop-up div over parent window
  • Can't safely lock a value of a ConcurrentDictionary
  • Android EditText change focus after validation and showing the error in a Dialog
  • Know of any tools to monitor the REAL performance of an amazon ec2 instance? [closed]
  • Display.getDefault().asyncExec not running correctly
  • NSWindowController circular dependency caused by bindings?
  • Android Studio memory usage Ubuntu 16
  • Dynamic programming matrix chain multiplication
  • String not fitting in label which is in a subview for swift
  • JVM Monitor char array memory usage
  • Defining Tables, Views, and Indexes in AWS CloudFormation
  • Condor job using DAG with some jobs needing to run the same host
  • How can I manually set the document id in a corpus?
  • jQuery Mobile - Dialogs without changing hash
  • inserting items in list in mongodb document
  • Mapping two workspace folders to the same working directory
  • Insert records if not exist SQL Server 2005
  • How to open multiple instances of a program in Linux
  • Manually Timing out a C# Thread
  • Accessing Rows In A LINQ Result Without A Foreach Loop?
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Python CGI os.system causing malformed header
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • Jquery - Jquery Wysiwyg return html as a string
  • R: gsub and capture
  • Confusion with PayPal's monthly billing cycle
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • How to disable jQuery.jplayer autoplay?
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?