How to start background task at boot - Windows Sto

2020-04-02 08:27发布

问题:

My tablet runs Windows 8.1 pro.

It has a background task which is triggered by a Time Trigger every 15'. It works, fair enough.

The problem is that I need to auto-launch my background task at every single boot (start app) of my device.

I registered my bg by this code:

       builder.Name = "bikePositionUpdate";
        builder.TaskEntryPoint = "BackgroundTaskGps.BikeGPSPositionUpdateBackgroundTask";
        builder.SetTrigger(new TimeTrigger(15, false)); // 

        // adding condition
        SystemCondition internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
        SystemCondition userPresentCondition = new SystemCondition(SystemConditionType.UserPresent); 

        builder.AddCondition(internetCondition);
        builder.AddCondition(userPresentCondition);
        BackgroundTaskRegistration taskRegistration = builder.Register();

my app has lock screen access

         await BackgroundExecutionManager.RequestAccessAsync();

How can I achieve this? Am I missing something?

回答1:

You have to add the SystemConditionType.SessionConnected condition, this condition happen every time the user log on to Windows.

An app must be placed on the lock screen before it can successfully register background tasks using this trigger type.

Edit:

On this url you can find the official documentation about what you need, and how to use it:

https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977056.aspx

https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.applicationmodel.background.systemtriggertype.aspx



回答2:

I think you should add SystemConditionType.SessionConnected condition,where this condition will check every time theuser log on to Windows


回答3:

Have you tried adding it to run on startup in the registry?

I dont have 8.1 to check but if hasnt changed from win7 the path should be HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run (or HKEY_LOCAL_MACHINE) just create a new string value with the path to your app and it will be run when windows starts



回答4:

result of this await BackgroundExecutionManager.RequestAccessAsync(); should be like AllowedWithAlwaysOnRealTimeConnectivity.

Which means : The user chose "allow" in the dialog box. The app is added to the lock screen, can set up background tasks.

And this BackgroundTaskRegistration taskRegistration = builder.Register(); you sholud call after await BackgroundExecutionManager.RequestAccessAsync();



回答5:

Have you tried adding your application to Windows Task Scheduler as part of the installation process?