Periodic task concurrency issue with foreground app

风格不统一 提交于 2019-12-25 07:17:16

问题


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 :

  • 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).

  • 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.

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?


回答1:


Mutex

The mutex option is officially recommended by the MSDN documentation 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.

NOTE: 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. Peter Torr wrote about it on the official Windows Phone Developer blog.


Enabled/Disable Background Agent

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.


Agent Renewal

Background Agents cannot renew themselves - once added, they will run for a maximum of two weeks unless renewed by the Foreground App. This will prevent Background Agents running for apps the user no longer uses.



来源:https://stackoverflow.com/questions/17896678/periodic-task-concurrency-issue-with-foreground-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!