Home > Articles > Programming > C#

  • Print
  • + Share This
From the author of Timer

Timer

The task by itself will never fire without a trigger. There are numerous types of triggers you can use. For this example, a TimeTrigger is used. The TimeTrigger will fire at a specified interval to enable your app to perform some work. In the example app, that work is simply generating a random number to update the badge and the tile. Create an instance of the trigger and pass the appropriate parameters to its constructor:

var trigger = new TimeTrigger(15, false);

The first value is the time, in minutes, to wait before running the task. This value cannot be less than 15 minutes. If you attempt to specify a shorter interval, an exception will be thrown when you register the related task. The second value determines whether the trigger is intended to be fired only once, or if it is a recurring trigger. A value of true indicates it should only be called once, and a value of false indicates it is recurring and should be called repeatedly.

Once you’ve created the trigger, you must update the background task with the trigger. This is done by calling the SetTrigger method. You may only have one trigger per background task.

builder.SetTrigger(trigger);

Triggers ordinarily fire immediately, so a time-based trigger will run every 15 minutes. Some tasks may not make sense to run even when the trigger is ready. For example, a task that fetches data from the Internet doesn’t need to run when the Internet is not available. A task that fetches large volumes of data should only run when the user is on a free network. You can control when your tasks run using a set of system-provided conditions.

  • + Share This
  • 🔖 Save To Your Account