Introduction
Task Scheduler is a class that schedules and automatically fires events at a time you specify. All important triggers are available: OnlyOneTime
, Daily
, Weekly
and Monthly
.
So why another Task Scheduler?
We need for one of our projects a scheduler that
- Can check a date or show all dates on which a trigger will fire. (See the demo - button "Show List")
- We need to schedule tasks not only daily, weekly,... we need a combination of them. e.g. every Wednesday (weekly trigger) plus every last day in a month (monthly trigger)
Update 1: Version 1.1
- Added date formatting during serialization/deserialization
- Fixed issue in TriggerItem.FromXML()
Update 2: Version 1.2
- Now using System.Timers.Timer instead of System.Windows.Forms.Timer
- Minor changes in XML serialization
- Demo now includes a Windows-Service with full service control (start/stop/install/uninstall)
- TriggerItemCollection support now serialization / deserialization
- Bug fixes
Features
- Simple structure and easy to use
- Possible triggers:
OneTimeOnly
, Daily
, Weekly
and Monthly
- Monthly trigger:
DayOfMonth
and Weekday
- Collection of TriggerItems
- Next Trigger-Date/Time information
- Check if a TriggerItem will fire on a certain date
CheckDate(DateTime date)
- Combine different triggers in one item. (e.g. every Friday plus every last day in a month)
If there is an overlap the Trigger will fire only one time - Save and restore
TriggerItems
to and from XML
Settings
Field | Description |
StartDate | Specifies the first date on which the trigger will fire |
EndDate | Specifies the last date on which the trigger will fire |
TriggerTime | Specifies the time on which the trigger will fire |
Enabled | Enable / disable the trigger |
TriggerSettings | Set the appropriate trigger-dates as described below |
TriggerItem.TriggerSettings
Overview:
To activate a specific date just set the appropriate flag(s):
triggerItem.TriggerSettings.Weekly.DaysOfWeek[(int)DayOfWeek.Sunday] = true;
triggerItem.TriggerSettings.Monthly.Month[(int)TaskScheduler.MonthOfTheYeay.January] =
true;
triggerItem.TriggerSettings.Monthly.Month[(int)TaskScheduler.MonthOfTheYeay.February] =
true;
triggerItem.TriggerSettings.Monthly.DaysOfMonth[(int)TaskScheduler.DayOccurrence.Last] =
true;
triggerItem.TriggerSettings.Monthly.WeekDay[(int)DayOfWeek.Sunday] = true;
Using TaskScheduler and TriggerItems
- Create a new instance of
TaskScheduler
.
TaskScheduler _taskScheduler = new TaskScheduler();
_taskScheduler.SynchronizingObject = this;
- Create a new Trigger-Item. Set start and end date + trigger time and if you like a tag.
TaskScheduler.TriggerItem triggerItem = new TaskScheduler.TriggerItem();
triggerItem.Tag = textBoxlabelOneTimeOnlyTag.Text;
triggerItem.StartDate = dateTimePickerStartDate.Value;
triggerItem.EndDate = dateTimePickerEndDate.Value;
triggerItem.TriggerTime = dateTimePickerTriggerTime.Value;
triggerItem.OnTrigger += new TaskScheduler.TriggerItem.OnTriggerEventHandler(
triggerItem_OnTrigger);
Settings for "OneTimeOnly
"
TriggerItem.TriggerSettings.OneTimeOnly.Active = checkBoxOneTimeOnlyActive.Checked;
triggerItem.TriggerSettings.OneTimeOnly.Date =
dateTimePickerOneTimeOnlyDay.Value.Date;
Settings for "Daily
"
triggerItem.TriggerSettings.Daily.Interval = (ushort)numericUpDownDaily.Value;
Settings for "Weekly
"
for (byte day = 0; day < 7; day++)
triggerItem.TriggerSettings.Weekly.DaysOfWeek[day] =
checkedListBoxWeeklyDays.GetItemChecked(day);
Settings for "Monthly
"
for (byte month = 0; month < 12; month++)
triggerItem.TriggerSettings.Monthly.Month[month] =
checkedListBoxMonthlyMonths.GetItemChecked(month);
for (byte day = 0; day < 32; day++)
triggerItem.TriggerSettings.Monthly.DaysOfMonth[day] =
checkedListBoxMonthlyDays.GetItemChecked(day);
for (byte weekNumber = 0; weekNumber < 5; weekNumber++)
triggerItem.TriggerSettings.Monthly.WeekDay.WeekNumber[weekNumber] =
checkedListBoxMonthlyWeekNumber.GetItemChecked(weekNumber);
for (byte day = 0; day < 7; day++)
triggerItem.TriggerSettings.Monthly.WeekDay.DayOfWeek[day] =
checkedListBoxMonthlyWeekDay.GetItemChecked(day);
- Add the trigger to the Collection and enable the Scheduler
triggerItem.Enabled = true;
_taskScheduler.AddTrigger(item);
_taskScheduler.Enabled = true;
Disclaimer
THE SOFTWARE AND THE ACCOMPANYING FILES ARE DISTRIBUTED "AS IS" AND WITHOUT ANY WARRANTIES WHETHER EXPRESSED OR IMPLIED. NO REPONSIBILITIES FOR POSSIBLE DAMAGES OR EVEN FUNCTIONALITY CAN BE TAKEN. THE USER MUST ASSUME THE ENTIRE RISK OF USING THIS SOFTWARE.