In my app both the app and a background task access a file and whichever opens the file first, the other one will fail.
to avoid that happening, I want to stop the background task when the user opens the app, and don't run the background if the app is already open.
How to know from background task if app is running or not? and how to stop it before app starts? thanks.
This is how I register the background task:
var result = await BackgroundExecutionManager.RequestAccessAsync();
var builder = new BackgroundTaskBuilder();
builder.Name = taskName;
builder.TaskEntryPoint = taskEntryPoint;
builder.SetTrigger(trigger);
builder.AddCondition(condition);
BackgroundTaskRegistration task = builder.Register();
There is a UserNotPresent
condition that specifies the task can run only if user is not present. But I don't understand by present it means user is working with the app, or with phone.