0

I am using a 3rd party rest api to query data and display it in my app. I have to perform a task like at night 12 approx. it will perform a background task to query data from rest api and update live tile and generate notification. I would like to use only C# only for this task. I don't know what will be best approach to do this task. But I using below code to perform background task to do this which is not working. Not sure why?

BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.Name = taskName;
SystemTrigger trigger = new SystemTrigger(SystemTriggerType.InternetAvailable, false);
taskBuilder.SetTrigger(trigger);
taskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
taskBuilder.TaskEntryPoint = typeof(BackgroundTask.BackgroundTask).FullName;
taskBuilder.Register();

and from background task I am querying data and generating toast notification.

Any help why this code is not working or when this task will fire. Is there any better approach to do above task?

Thanks

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
sunder
  • 1,803
  • 4
  • 29
  • 50
  • 1
    You may take a look at *TimeTrigger* which is well [explained at MSDN](http://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj553413.aspx), [this answer](http://stackoverflow.com/a/24076237/2681948) may also help and you may find something usefull also [at this post](http://www.romasz.net/how-to-add-a-backgroundtask/). – Romasz Aug 22 '14 at 06:14

3 Answers3

2

Regarding the code you have not working...

For Windows Phone 8.1 unlike Windows 8\8.1, you are required to call BackgroundExecutionManager.RequestAccessAsync() (search MSDN\internet) for ANY background task before registering task(s) whereas in Windows this is only required for some tasks. Make sure your code calls this and validate the returned value before registering your background task(s).

Regarding knowing if your task "worked"...

It's a good idea to have the background task implementation run (IBackgroundTask::Run()) independent of the trigger\conditions you've set to ensure it performs without issue by debugging it. See instructions in the following link: http://msdn.microsoft.com/en-US/library/windows/apps/xaml/jj542416.aspx.

Regarding your use of SystemConditionType.InternetAvailable...

I'm not 100% about this but I'm pretty certain this will be redundant given you already have a SystemTriggerType.InternetAvailable. I don't know of a situation where the trigger would fire but the condition wouldn't be true.

Regarding the requirement you've mentioned...

If I understand your requirement correctly you have different options here:

  1. If your app is a Windows Phone XAML app that need to run based on time, I would recommend either TimeTrigger or MaintenanceTrigger triggers (as opposed to the SystemTrigger). These are both Background Tasks. For general info on Background Tasks and links to the TimeTrigger and MaintenanceTrigger documentation see this MSDN link: http://msdn.microsoft.com/en-US/library/windows/apps/xaml/hh977056.aspx.
  2. If your app is a Windows Phone Silverlight 8.0 app you can use Background Agents, specifically either PeriodicTask or ResourceIntensiveTask. See the links posted by others or search the MSDN\internet for more info.
  3. If your app is a Windows Phone Silverlight 8.1 app you can use the option in either 1 or 2 above.
Jimmy Alexander
  • 445
  • 2
  • 9
1

I think you should try using PeriodicTask. Also consider the constraints mentioned in the link.

bit
  • 4,407
  • 1
  • 28
  • 50
  • Yes that's a good alternative for TimeTask. I guess what @Romasz have suggested is great solution and i'll add InternetConnectivity condition with it. – sunder Aug 22 '14 at 06:54
0

create one class with output Type :Windows Runtime Component and put your Class that inheritance from IBackroundTask so this work if you use from emulator for launching app, i think your app for register task not active in emulator.

Mojtaba Safavi
  • 127
  • 2
  • 10