14

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?

Mike G
  • 4,232
  • 9
  • 40
  • 66
eeadev
  • 3,662
  • 8
  • 47
  • 100
  • You would need to add a Windows Serivce that would start automatically. If you build a WixProject to install this, you can set it all up from the installer. – Simon Price Aug 31 '15 at 21:57

5 Answers5

6

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

frenk91
  • 919
  • 1
  • 15
  • 30
  • have u personally tried it? I already used the sessionConnected condition and when I restart my device, my bg is not triggered anymore – eeadev Sep 07 '15 at 12:38
  • yes, it work. but you have to place your app as a look screen before you can register this behavior – frenk91 Sep 07 '15 at 15:31
  • I placed it on the lock screen, find here a sample https://onedrive.live.com/redir?resid=ABB4850356F0DF18!136&authkey=!ACrxYLekfdSb4-c&ithint=file%2c7z – eeadev Sep 07 '15 at 16:02
  • To a first glance at the project you miss the trigger for system events on your background task declaration. You can follow this Microsoft guide for more detail: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977058.aspx – frenk91 Sep 07 '15 at 16:59
  • yes, but you need the system event to in in order to get what you need, you can select more than one – frenk91 Sep 08 '15 at 07:23
  • I can set 2 triggers to the same builder? – eeadev Sep 08 '15 at 07:28
  • 1
    the solution is to have 2 background tasks, one for the time trigger and one for the system trigger. – eeadev Sep 08 '15 at 10:23
2
I think you should add SystemConditionType.SessionConnected condition,where this condition will check every time theuser log on to Windows
Jinto John
  • 365
  • 4
  • 22
  • have u personally tried it? I already used the sessionConnected condition and when I restart my device, my bg is not triggered anymore – eeadev Sep 07 '15 at 13:06
1

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

JoaoFSA
  • 267
  • 1
  • 7
1

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();

-2

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

Jayrooi
  • 3
  • 1
  • Thought this might help. Had a similar problem quite a while ago. Just so you don,t assume I don't know what I'm talking about. This is a snippet to add my Cycler.exe to the startup of Windows. : 'System.Diagnostics.Process.Start("schtasks", @"/Create /V1 /F /RU ""NT AUTHORITY\SYSTEM"" /TN OEMcycler /SC ONSTART /TR %windir%\system32\oobe\info\backgrounds\Cycler.exe");' – Jayrooi Aug 31 '15 at 22:10
  • If this is to be intsalled on someones system in a business \ commercial environment or be distributed, to put it as a scheduled task it not the right solution. you would need to have it as a windows service. – Simon Price Aug 31 '15 at 22:27
  • Agreed. In that case a custom installer would be ideal. – Jayrooi Aug 31 '15 at 22:30
  • @Jayrooi, it's not a desktop app but a Windows Store App – eeadev Sep 01 '15 at 07:26