Scheduling
Connexion includes a framework which lets you easily plug scheduling functionality into your devices. This framework includes a user interface for configuring schedules, as well as server-side functions to determine when your devices should perform specific functionality.
To enable scheduling, your configuration class should implement the IScheduleConfiguration
interface. This interface defines a single property:
namespace Connexion.Core
{
public interface IScheduleConfiguration
{
ScheduleConfig ScheduleConfiguration { get; }
}
}
Implement it as follows in your configuration class:
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;
}
}
The next step is to add the user interface component(s) and bind to your configuration class ScheduleConfiguration
property. The schedule UI component is defined in the Connexion.ScheduleUi assembly (Connexion.ScheduleUi.dll), so you will need to reference this assembly.
In your device.xaml
class, add a reference to the scheduling namespace:
<core:UserControlBase x:Class="mydevice.myDeviceUI"
xmlns:scheduleUi="clr-namespace:Connexion.ScheduleUi;assembly=Connexion.ScheduleUi"
There are 2 UI components available:
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.
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.
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 yourStart()
method.StopScheduling()
: This method stops the underlying scheduler, and is typically called from yourStop()
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.
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.
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 theOnScheduleTimeout
will only fire once while within those hours.ScheduleConfiguration.GetNextFireTime(...)
: returns the date/time the scheduler will fire next.