0

I am trying do background task to run once a day, but not work, I tried with TimeTrigger, but not work.

I am try SystemTrigger but not working

 //  var trigger = new TimeTrigger(15, false);
    var trigger = new SystemTrigger(SystemTriggerType.TimeZoneChange, false);
    var condition = new SystemCondition(SystemConditionType.InternetAvailable);

    var tarefa = await RegistrarTarefasSegundoPlanoAsync.RegisterBackgroundTask(typeof(SalvaImagemTask).FullName, "SalvaImagemTask", trigger, condition);
Marcos Costa
  • 337
  • 4
  • 14

2 Answers2

1

You can do that by implementing some logic.

  1. Register background task
  2. Make the BackgroundTask to run in every 1 hour i.e. 60 minutes interval like this:

    BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder { Name = "MyBackgroundTask", TaskEntryPoint = "MyRuntimes.BackgroundTask" }; taskBuilder.SetTrigger(new TimeTrigger(60, false)); taskBuilder.SetTrigger(trigger); BackgroundTaskRegistration myFirstTask = taskBuilder.Register();

  3. Inside your BackgroundTask, check your system local time, if its morning time, you can start your desired function.

This is best option I can think of as you cannot run your background task in the morning. But logically you run your background task in every 1 hour interval and check the condition if its morning time.

Hope you got my logic. Thanks!

Kishor Bikram Oli
  • 1,748
  • 1
  • 22
  • 40
  • Good, i did this. :) but The app calendar notify always 12PM. How i do this? Is possible create a background task inside background task with a count hour remaining to morning? – Marcos Costa Aug 30 '15 at 02:37
  • Background task inside background task? That sounds pretty interesting. Give it a try :) .. There is another approach to notify you exact at 12 PM. You can use Push Notification for that. There is an option that triggers background task instantly when you receive push notification in your phone. – Kishor Bikram Oli Aug 30 '15 at 15:43
  • Another approach I can think of is: As you know, the minimum background trigger time is 15 minutes. It means background task will start between 0-15minutes and in every interval. What you can do is, use do-while loop to run BackgroundTask continuously for 15 times or more in 15 minutes. That means your background task checks continuously with the current time. If it matches with 12PM, you can show notification. Hope you got my point. :) – Kishor Bikram Oli Aug 30 '15 at 15:51
-1

There is a very similar question that was answered here. It uses Windows Scheduled Tasks.

Community
  • 1
  • 1
Chris Etheridge
  • 111
  • 1
  • 2