How to detect if the main app is running from a background agent

蓝咒 提交于 2020-01-16 08:54:48

问题


I'm working on a WP7.5 app where I use a background agent to update tiles in the background (when the app is not running).

In the OnInvoke method of the agent, is there a way to know if the main app is currently running ?

I'd like to detect that in order to NOT updating the tiles if the app is running because I already udpate them when the app exits.

Thank you.


回答1:


Your only choice is to write use isolated storage to communicate, since it's shared by both agent and application.

Have the main application create a blank file on startup, and delete it when it closes. Your agent can then determine if the application is running by checking the existance of that file.




回答2:


This MSDN page not only lists unsupported APIs for background agents, but also emphasizes some APIs which you are encouraged to use. Among them is the Mutex:

The Mutex class should be used to synchronize access to resources that are shared between the foreground application and the background agent, such as files in isolated storage.

This should be exactly what you want. Create a named Mutex in your main application and try to open it in the background agent. If this succeeds then your main app is running, otherwise not.



来源:https://stackoverflow.com/questions/7817094/how-to-detect-if-the-main-app-is-running-from-a-background-agent

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