Reputation: 572
I want to run a background task for every 10 mins.As the UWP app will not always be opened.Is there a way to run a background task even when the app is closed(Not Running State)? If the answer is NO, Is there any another way other than Windows Services?
Upvotes: 2
Views: 2384
Reputation: 2304
Is there a way to run a background task even when the app is closed(Not Running State)?
According to this document:
You can use background tasks to provide functionality when your app is suspended or not running.
The 'not running' state in UWP is described in more detail here. It reads:
An app could be in this state because it hasn't been launched since the last time the user rebooted or logged in. It can also be in this state if it was running but then crashed, or because the user closed it earlier.
This implies that the app simply needs to run once after installation to be able to successfully register a background task, which can then run even after your app is closed.
I want to run a background task for every 10 mins
Not sure I'm interpreting this correctly but I assume you mean you want to run some bit of code every 10 mins while your app is not running. Well there are two way that I can think of to achieve this.
Method 1:
While background tasks are meant to be very short-lived tasks, they may even be made to run indefinitely if:
extendedBackgroundTaskTime capability is added as a restricted capability in your app's manifest file
Using this technique along with a simple timer mechanism would achieve the desired result.
Method 2 (more complicated but keeps background tasks short-lived):
Setup a DatagramSocket to a remote server which sends some data every 10 mins and register your background task with a SocketActivityTrigger.
Upvotes: 0
Reputation: 1967
You can create BackgroundTask
which run at the most every 15 minutes not less than that in UWP apps. for more details you can check this source.
Upvotes: 4