...
To enable scheduling, your configuration class should implement the IScheduleConfiguration
interface. This interface defines a single property:
Code Block |
---|
|
namespace Connexion.Core
{
public interface IScheduleConfiguration
{
ScheduleConfig ScheduleConfiguration { get; }
}
} |
Implement it as follows in your configuration class:
Code Block |
---|
|
public class MyDeviceConfiguration : NotifyBase, IScheduleConfiguration
{
private ScheduleConfig m_ScheduleConfiguration;
[DataMember]
public ScheduleConfig ScheduleConfiguration
{
get
{
if (m_ScheduleConfiguration == null)
{
m_ScheduleConfiguration = new ScheduleConfig();
m_ScheduleConfiguration.PropertyChanged += (o, e) => RaisePropertyChanged(e);
}
return m_ScheduleConfiguration;
}
} |
...
An icon adorner which will launch an ‘edit schedule’ popup window - this can be placed next to textboxes and similar controls to indicate additional scheduling functionality.

Code Block |
---|
|
<scheduleUi:SchedulerIcon ScheduleConfiguration="{Binding Config.ScheduleConfiguration}"
VerticalAlignment="Center" /> |
A schedule UI which can be placed directly onto your device UI - this can be placed on a separate tab or other large area if you only need a single schedule configuration.

Code Block |
---|
|
<Border>
<scheduleUi:ScheduleControl ScheduleConfiguration="{Binding Config.ScheduleConfig}" />
</Border> |
Once the ScheduleConfiguration UI is bound to your configuration class, no further work is required on your device.xaml file.
The next step is to implement your scheduling logic in your device class. There are three important methods
StartScheduling()
: This method starts the underlying scheduler, and is typically called from your Start()
method.
StopScheduling()
: This method stops the underlying scheduler, and is typically called from your Stop()
method.
OnScheduleTimeout(OnScheduleTimeoutArgs args)
: This method is called when the underlying scheduler triggers. Override this method and include any logic to be run when the schedule expires.
Code Block |
---|
|
public override void Start()
{
// your start logic
...
StartScheduling();
} |
Code Block |
---|
|
public override void Stop()
{
StopScheduling();
// your stop logic
...
} |
Code Block |
---|
|
protected override async Task OnScheduleTimeout(OnScheduleTimeoutArgs args)
{
// your logic to run when the schedule expires
// (read from file/database and post on channel, for example)
// or just flip a bit to let your device know it's within a scheduling period
_IsWithinSchedule = true; // use this to determine if you can process messages or must sleep
} |
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 |
---|
|
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.