I have a background task that I want to behave differently if the application is running. How can I detect if my Windows 8 store application is running?
Asked
Active
Viewed 109 times
0
-
1I think this has already been answered. http://stackoverflow.com/questions/18342640/how-to-check-from-scheduled-agent-if-the-foreground-app-is-running – kindasimple Jan 14 '14 at 19:48
-
1Additionally, use a mutex to avoid any case where two threads are accessing the same resources. – kindasimple Jan 14 '14 at 19:50
-
The linked duplicate is about windows phone and does not have a good answer, hence I am not voting to close this as a duplicate. – Ian Ringrose Feb 06 '14 at 10:26
2 Answers
0
Assuming that you want your app to behave differently when it is in focus, here is how I handled it in my code -
In your class constructor -
CoreWindow.GetForCurrentThread().Activated += CoreWindowOnActivated;
Then, add the implementation -
private void CoreWindowOnActivated(CoreWindow sender, WindowActivatedEventArgs args)
{
if (args.WindowActivationState == CoreWindowActivationState.CodeActivated)
{
//add your app specific behavior here
}
}
You will also need to detach the event when appropriate.

Jisha Puniyani
- 84
- 4
0
If I was doing this in WinForms I would use a Mutex,
foreground app can lock it
When the foreground app exits for any reason, then windows will unlock the Mutex
The background app can check if the Mutex is locked.
Sorry I don’t know I this work for a windows store app.

Ian Ringrose
- 51,220
- 55
- 213
- 317