Click here to Skip to main content
15,878,871 members
Articles / Programming Languages / C#

Task Scheduler

Rate me:
Please Sign up or sign in to vote.
4.86/5 (84 votes)
18 Apr 2015CPOL2 min read 289.7K   25.5K   304   73
A simple structured native task scheduler

Image 1

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

  1. Can check a date or show all dates on which a trigger will fire.  (See the demo - button "Show List")
  2. 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:
Another_TaskScheduler/TriggerSettings.jpg

To activate a specific date just set the appropriate flag(s):

// Activate Sunday on weekly trigger.
triggerItem.TriggerSettings.Weekly.DaysOfWeek[(int)DayOfWeek.Sunday] = true;

// Activate last Friday in January and February
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

  1. Create a new instance of TaskScheduler.
    // Create the TaskScheduler
    TaskScheduler _taskScheduler = new TaskScheduler();
    
    // Set the synchronizing object to get trigger events within the main thread.
    // Important if you're using Windows Forms
    _taskScheduler.SynchronizingObject = this;
  2. 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;
    // And the trigger-Event :)
    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++) // Set the active Days
    triggerItem.TriggerSettings.Weekly.DaysOfWeek[day] = 
        checkedListBoxWeeklyDays.GetItemChecked(day);

    Settings for "Monthly"

    for (byte month = 0; month < 12; month++) // Set the active Months
        triggerItem.TriggerSettings.Monthly.Month[month] = 
        checkedListBoxMonthlyMonths.GetItemChecked(month);
    
    // Set the active Days (0..30 = Days, 31=last Day) for monthly trigger
    for (byte day = 0; day < 32; day++)
        triggerItem.TriggerSettings.Monthly.DaysOfMonth[day] = 
        checkedListBoxMonthlyDays.GetItemChecked(day);
    
    // Set the active weekNumber and DayOfWeek
    // e.g the first monday, or the last friday...
    // 0..4: first, second, third, fourth or last week
    
    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);
  3. Add the trigger to the Collection and enable the Scheduler
    triggerItem.Enabled = true; // Set the Item-Active - State
    _taskScheduler.AddTrigger(item); // Add the trigger to List
    _taskScheduler.Enabled = true; // Start the Scheduler

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
p++ Datentechnik GmbH
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUse the static method TaskScheduler.TriggerItem.FromXML??? Pin
Member 949456625-Mar-13 11:44
Member 949456625-Mar-13 11:44 
GeneralThanks For This Perfect Article Pin
sagar savsani5-Jan-13 20:34
sagar savsani5-Jan-13 20:34 
QuestionFromXML Pin
wwwwe13-Nov-12 6:58
wwwwe13-Nov-12 6:58 
AnswerRe: FromXML Pin
Lothar Perr13-Nov-12 19:46
Lothar Perr13-Nov-12 19:46 
GeneralRe: FromXML Pin
wwwwe14-Nov-12 3:40
wwwwe14-Nov-12 3:40 
GeneralRe: FromXML Pin
Lothar Perr6-Dec-12 9:04
Lothar Perr6-Dec-12 9:04 
Questiontriggering exe Pin
markykoyski12-Nov-12 17:46
markykoyski12-Nov-12 17:46 
AnswerRe: triggering exe Pin
Lothar Perr13-Nov-12 19:55
Lothar Perr13-Nov-12 19:55 
do you want to start a windows service?
look at this article from MrPlentl:
Using the ServiceController in C# to stop and start a service[^]
GeneralGreat Job Pin
otakucrono20-Jul-10 12:20
otakucrono20-Jul-10 12:20 
GeneralRe: Great Job Pin
Lothar Perr27-Jul-10 21:37
Lothar Perr27-Jul-10 21:37 
GeneralRe: Great Job Pin
vbytesdc3-Mar-11 9:48
vbytesdc3-Mar-11 9:48 
GeneralNice work ..plz help for my struggle Pin
skyramesh22-Jan-10 23:03
skyramesh22-Jan-10 23:03 
GeneralRe: Nice work ..plz help for my struggle Pin
Lothar Perr3-Feb-10 5:25
Lothar Perr3-Feb-10 5:25 
GeneralReally Nice article... Pin
Kushagra Tiwari7-Sep-09 2:49
Kushagra Tiwari7-Sep-09 2:49 
GeneralNice work Pin
Xmen Real 7-Aug-09 14:42
professional Xmen Real 7-Aug-09 14:42 
GeneralRe: Nice work Pin
Lothar Perr8-Aug-09 4:17
Lothar Perr8-Aug-09 4:17 
GeneralRe: Nice work Pin
Donsw21-Aug-09 16:20
Donsw21-Aug-09 16:20 
GeneralRe: Nice work Pin
Lothar Perr21-Aug-09 21:11
Lothar Perr21-Aug-09 21:11 
GeneralNo discription of TaskScheduler class Pin
flow19655-Aug-09 8:14
flow19655-Aug-09 8:14 
GeneralRe: No discription of TaskScheduler class Pin
Lothar Perr5-Aug-09 11:45
Lothar Perr5-Aug-09 11:45 
GeneralSuggestion [modified] Pin
Syed Hasan Hyder4-Aug-09 7:09
Syed Hasan Hyder4-Aug-09 7:09 
GeneralRe: Suggestion Pin
Lothar Perr4-Aug-09 8:15
Lothar Perr4-Aug-09 8:15 
GeneralMy vote of 2 Pin
Country Man4-Aug-09 4:20
Country Man4-Aug-09 4:20 
RantRe: My vote of 2 Pin
jgauffin4-Aug-09 4:49
jgauffin4-Aug-09 4:49 
GeneralRe: My vote of 2 Pin
Lothar Perr4-Aug-09 7:55
Lothar Perr4-Aug-09 7:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.