2

I have created a background task in UWP using WindowsRuntimeComponent. And register the BackgroundTask in UWP App "OnNavigatedTo" event with timer. It is working fine. Here my question is, Is this the only way to register BackgroundTask or can we host this as like Windows service? So that it can run automatically when machine starts with our UWP App start.

Can anybody help on this to achieve this?

demonplus
  • 5,613
  • 12
  • 49
  • 68
Suresh K
  • 131
  • 1
  • 6
  • Check this: https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-a-background-task?f=255&MSPPError=-2147217396 and this: http://stackoverflow.com/questions/32229375/how-to-start-background-task-at-boot-windows-store-app – Kinani Oct 07 '16 at 11:11

1 Answers1

2

so that it can run Automatically when machine start with out UWP App start.

There are two approaches to implementing background tasks: in-process and out-of-process. In-process background support was introduced in Windows 10, version 1607, to simplify writing background tasks.

For single-process background tasks, it's life-cycle is influenced by the app life cycle, as you concerned, after you restart the machine you need to start the uwp app to run the tasks. More details please see Create and register a single process background task. But for multi-process background tasks,once you register the background task with time trigger, it will always run until you unregistered it. After you restart the machine, the task will start automatically without starting the app. More details please see Create and register a background task that runs in a separate process.

As you created a windows runtime component for the background task TaskEntryPoint, I think the background task you created is separate process. So you can test it to see if the background task can run after the machine restart without app start(pay attention you didn't set any conditions which may block the task). For example, I wrote a separate process background task with a time trigger to write current time to a text file every 15 minutes. After I closed the machine and started it on the second day it will go on writing data to the file without app starting. Here is parts of my result:

10/10/2016 5:47:25 PM second activity10/10/2016 6:02:25 PM first activity
10/10/2016 6:02:25 PM second activity10/11/2016 9:04:19 AM first activity
10/11/2016 9:04:19 AM second activity10/11/2016 9:27:44 AM first activity
10/11/2016 9:27:44 AM second activity10/11/2016 9:42:44 AM first activity

More details about background task please reference Guidelines for background tasks and the official sample.

Is this the only way to register BackgroundTask or can we host this as like Windows service?

Windows service has many characteristics such as no UI and run as long as windows is running. For these, sounds like app service has similar characteristics. An app service is implemented as a background task.This enables a foreground application to invoke an app service in another application to perform tasks behind the scenes. Once you deployed the app service provider on your machine, you can call the background task in anytime from your client app without launch the service provider app. But this background task will have same life cycle with your foreground client app, it means if you don't start the client app, the app service cannot be connected. More details please reference Create and consume an app service.

In total, a background task in a separate process can meet your requirements.

Sunteen Wu
  • 10,509
  • 1
  • 10
  • 21