0

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?

bryanbcook
  • 16,210
  • 2
  • 40
  • 69
  • 1
    I 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
  • 1
    Additionally, 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 Answers2

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.

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