T W
T W

Reputation: 6317

Background task and running app detection

I have background task in my app, which runs every 15 minutes (when on lock screen). Is it possible to detect if my app is running when background task is executing ? I'd like to skip background task execution in such case, to avoid concurrency problems.

Upvotes: 5

Views: 347

Answers (3)

Kristian Williams
Kristian Williams

Reputation: 2343

Have you considered a shared Mutex? This would save from horrible situations where (for example) the background task fails and doesn't clear it's active flag, or the localSettings becomes corrupted.

Upvotes: 0

Tim
Tim

Reputation: 459

You can't detect when your app is running from a background task but you can detect when the background task is running from your app with the BackgroundTaskRegistration Progress and Completed events.

This sample covers everything you should need. http://code.msdn.microsoft.com/windowsapps/Background-Task-Sample-9209ade9

Upvotes: 2

Because both the app and the background task have access to app data, have the app store a flag in ApplicationData.localSettings when it's activated, clearing the flag when it's suspended, and resetting the flag when it's resumed. The background task can then check that flag when it starts and make the decision from there.

Upvotes: 1

Related Questions