Check if the application is running from a background task

I am creating a UWP application with a background task. I do not want to run the background when the real application is running or paused. Is there any way to achieve this? Ive looked at SystemConditionType , but nothing matches what I'm looking for.

0
win-universal-app windows-10-universal
source share
1 answer

Since background tasks (except for special ones) are performed in a separate process, there is no elegant way to check whether an application works or not, since memory is not shared with the application and background task. I have successfully completed two methods:

Application service

Create the application-application in the application, in the background task, try connecting to the service. If the service is available, it means that the application is running. This is a favorite when you need a connection between a task and an application.

File lock

This is your favorite in simple scenarios that you just need to know if the application is working or not.

  • Each time you launch the application, create / open a file in the local folder of the application, open it for recording and save it while the application is running. Close it when the event is paused and reopen it when the event resumes.
  • In the background task, try to open the file for writing, if the attempt was successful, this means that the application is not running.
  • Close the file immediately in the background job.

Note: In the new API, available in the anniversary update, background tasks can be performed in the same process as the application . Using the new model, you will no longer have this problem.

+5
source share

All Articles