Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info

If your OnScheduleTimeout method is long running, you should call StopScheduling() at the beginning and StartScheduling() in a finally block to ensure you don’t end up with multiple threads within OnScheduleTimeout.

Polling Intervals

In addition to ‘waking up’ at certain times, the scheduling classes can be used to support scheduled polling. For example, if you want to perform an operation (like checking for new file) every X seconds between Y and Z hours.

To enable polling, set the PollingPeriodInSeconds property. You can also use the StartupDelayInMilliseconds property if you wish to delay the first poll.

Code Block
languagec#
public override void Start()
{
  // ... your logic ...     

  ScheduleConfiguration.PollingPeriodInSeconds = Configuration.PollingInterval;

  if (ScheduleConfiguration.PollingPeriodInSeconds <= 0)
  {
    Logger.Error(0, $"Polling interval invalid: {ScheduleConfiguration.PollingPeriodInSeconds}. Defaulting to 60 seconds");
    ScheduleConfiguration.PollingPeriodInSeconds = 60; // set it to the default
  }
  
  // delay the first poll to allow the system to fully start up
  ScheduleConfiguration.StartupDelayInMilliseconds = StartupDelayInMilliseconds > 0 ? StartupDelayInMilliseconds : 5 * 1000;
  StartScheduling();
}

Schedule Configuration Methods

The ScheduleConfiguration class contains a number of methods and properties which can help you leverage more advanced scheduling.

  • ScheduleConfiguration.IsWithinSchedule(DateTime dateTime): Pass in any DateTime to determine if it’s within the configured schedule. For example, if you have a timer which fires every X seconds, you can determine if the current time is within a configured schedule.

  • ScheduleConfiguration.AlwaysSkipToNextSchedule: If you only want to perform an operation once per schedule, then set this property to true. For example, if you have a schedule configured between hours X and Y each day, then the OnScheduleTimeout will only fire once while within those hours.

  • ScheduleConfiguration.GetNextFireTime(...): returns the date/time the scheduler will fire next.