Dodeius
Dodeius

Reputation: 125

How do I make my Backgroundtask start on Startup?

I have a background task, that is currently launched whenever my application starts and it is currently not running. But I would like it to also start on startup, if that's possible. I have looked into the triggers, and there appears to be none that can make it start on startup. According to MSDN there is a way to make a startup task, but they don't say how to convert my current task into a startup task. Is that even possible:

<uap5:Extension Category="windows.startupTask">
  <uap5:StartupTask
    TaskId="MyStartupId"
    Enabled="true"
    DisplayName="Background-Keyboard task" />
</uap5:Extension> //Documentation

<Extension Category="windows.backgroundTasks" EntryPoint="Background.BackgroundTask">
  <BackgroundTasks>
    <Task Type="systemEvent" />
  </BackgroundTasks>
</Extension> //My task

I currently initiate my task like this:

var builder = new BackgroundTaskBuilder();

builder.Name = exampleTaskName;
builder.TaskEntryPoint = "Background.BackgroundTask";

ApplicationTrigger _AppTrigger = new ApplicationTrigger();
builder.SetTrigger(_AppTrigger);


builder.Register();

await _AppTrigger.RequestAsync();

Edit: My reason for wanting this:

My app provides a background service that can be toggled on and off. It should be running all the time, and that does work after I open the app. But it should not be necessary to open it every time the device is restarted - which is currently the case, and I want to add the possibility to let the background service start at startup. Ideally I would also want to keep the current way of launching it - via _AppTrigger.RequestAsync();, so my enable/disable slider keeps working.

Upvotes: 1

Views: 503

Answers (1)

Stefan Wick  MSFT
Stefan Wick MSFT

Reputation: 13850

As some of the comments have already indicated, you should use a SystemTrigger of TriggerType=SessionConnected to have the task start when the user logs on. You can still also use the ApplicationTrigger to trigger the task from your foreground app whenever that is needed. You can have two or more triggers with the same entry point, triggering the same task implementation. In fact, in order to make this scenario complete you also want to add a SystemTriggerType=PowerChanged trigger to cover power/sleep state changes.

Upvotes: 1

Related Questions