问题
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