Click here to Skip to main content
Click here to Skip to main content
Go to top

TaskScheduler

, 17 Apr 2013
Rate this:
Please Sign up or sign in to vote.
A simple structured and easy to use Task-Scheduler

Introduction

Task Scheduler is a class that schedules and automatically fires events at a time you specify. All important triggers are implemented: 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 the 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. f.e. every Wednesday (Weekly Trigger) plus every last day in a Month (Monthly Trigger)

Update 1:

  • Added date formatting during serialization/deserialization
  • Fixed TriggerItem-FromXML issue

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. (f.e. every Friday plus every last Day in a Month)
    If there is an overlap the Trigger fires 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();
  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)

Share

About the Author

Lothar Perr
Team Leader Call-Data-Systems
Germany Germany
No Biography provided

Comments and Discussions

 
QuestionNot Triggering the Event :S PinmemberUngarMax014-Dec-13 11:07 
Questionhow to integrate window services with this application.!! [modified] PinmemberVarun_nayak22-Nov-13 22:18 
Questionhelp me!How to create a task in Windows Xp,using Microsoft.Win32.TaskScheduler.dll. PinmemberMember 1041057420-Nov-13 20:52 
BugWon't run as Windows Service Pinmemberrix_27-Sep-13 6:21 
GeneralRe: Won't run as Windows Service PinmemberVarun_nayak22-Nov-13 22:37 
GeneralMy vote of 5 Pinmembermyeric12-Aug-13 21:33 
Questionif on time run task system off after system do on are run task ??? Pinmemberali21232111224-Jul-13 0:43 
AnswerRe: if on time run task system off after system do on are run task ??? PinmemberLothar Perr24-Jul-13 1:06 
QuestionHow to schedule trigger even if application terminated like window task schedule PinmemberVarun_nayak19-Jul-13 22:31 
AnswerRe: How to schedule trigger even if application terminated like window task schedule PinmemberLothar Perr24-Jul-13 1:03 
GeneralRe: How to schedule trigger even if application terminated like window task schedule [modified] PinmemberVarun_nayak22-Nov-13 22:22 
QuestionTriggerOneTimeOnly bug?? Pinmemberthomas3.1423-Apr-13 12:50 
QuestionHelp PinmemberBill Rady23-Apr-13 7:36 
GeneralMy vote of 4 PinmemberPrasad Khandekar17-Apr-13 9:23 
QuestionUse the static method TaskScheduler.TriggerItem.FromXML??? [modified] PinmemberMember 949456625-Mar-13 11:44 
GeneralThanks For This Perfect Article Pinmembersagar savsani5-Jan-13 20:34 
QuestionFromXML Pinmemberwwwwe13-Nov-12 6:58 
AnswerRe: FromXML PinmemberLothar Perr13-Nov-12 19:46 
GeneralRe: FromXML Pinmemberwwwwe14-Nov-12 3:40 
GeneralRe: FromXML PinmemberLothar Perr6-Dec-12 9:04 
Questiontriggering exe Pinmembermarkykoyski12-Nov-12 17:46 
AnswerRe: triggering exe PinmemberLothar Perr13-Nov-12 19:55 
GeneralGreat Job Pinmemberotakucrono20-Jul-10 12:20 
GeneralRe: Great Job PinmemberLothar Perr27-Jul-10 21:37 
GeneralRe: Great Job Pinmembervbytesdc3-Mar-11 9:48 
GeneralNice work ..plz help for my struggle Pinmemberskyramesh22-Jan-10 23:03 
GeneralRe: Nice work ..plz help for my struggle PinmemberLothar Perr3-Feb-10 5:25 
GeneralReally Nice article... PinmemberKushagra Tiwari7-Sep-09 2:49 
GeneralNice work PinmemberXmen W.K.7-Aug-09 14:42 
GeneralRe: Nice work PinmemberLothar Perr8-Aug-09 4:17 
GeneralRe: Nice work PinmemberDonsw21-Aug-09 16:20 
GeneralRe: Nice work PinmemberLothar Perr21-Aug-09 21:11 
GeneralNo discription of TaskScheduler class Pinmemberflow19655-Aug-09 8:14 
GeneralRe: No discription of TaskScheduler class PinmemberLothar Perr5-Aug-09 11:45 
GeneralSuggestion [modified] PingroupHasan Hyder4-Aug-09 7:09 
GeneralRe: Suggestion PinmemberLothar Perr4-Aug-09 8:15 
GeneralMy vote of 2 PinmemberCountry Man4-Aug-09 4:20 
RantRe: My vote of 2 Pinmemberjgauffin4-Aug-09 4:49 
GeneralRe: My vote of 2 PinmemberLothar Perr4-Aug-09 7:55 
GeneralMy vote of 5 PinmemberSpiff Dog4-Aug-09 10:11 
GeneralRe: My vote of 2 PinmemberXmen W.K.7-Aug-09 14:39 
GeneralWe have done one of these for work, and I know what this feels like PinmvpSacha Barber2-Aug-09 21:38 
GeneralRe: We have done one of these for work, and I know what this feels like PinmemberLothar Perr4-Aug-09 8:14 
Generalnice Pinmemberpimb228-Jul-09 7:02 
GeneralRe: nice PinmemberLothar Perr28-Jul-09 7:27 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 17 Apr 2013
Article Copyright 2009 by Lothar Perr
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid