Click here to Skip to main content
13,139,569 members (54,370 online)
Click here to Skip to main content
Add your own
alternative version


295 bookmarked
Posted 28 Jul 2009

Task Scheduler

, 18 Apr 2015
Rate this:
Please Sign up or sign in to vote.
A simple structured native task scheduler


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


  • 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


StartDateSpecifies the first date on which the trigger will fire
EndDateSpecifies the last date on which the trigger will fire
TriggerTimeSpecifies the time on which the trigger will fire
EnabledEnable / disable the trigger
TriggerSettingsSet the appropriate trigger-dates as described below

TriggerItem.TriggerSettings Overview:

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] = 
triggerItem.TriggerSettings.Monthly.Month[(int)TaskScheduler.MonthOfTheYeay.February] = 
triggerItem.TriggerSettings.Monthly.DaysOfMonth[(int)TaskScheduler.DayOccurrence.Last] =
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(

    Settings for "OneTimeOnly"

    TriggerItem.TriggerSettings.OneTimeOnly.Active = checkBoxOneTimeOnlyActive.Checked;
    triggerItem.TriggerSettings.OneTimeOnly.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] = 

    Settings for "Monthly"

    for (byte month = 0; month < 12; month++) // Set the active Months
        triggerItem.TriggerSettings.Monthly.Month[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] = 
    // 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] = 
    for (byte day = 0; day < 7; day++)
        triggerItem.TriggerSettings.Monthly.WeekDay.DayOfWeek[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




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


About the Author

Lothar Perr
p++ Datentechnik GmbH
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionIt has Snooze mode? Pin
Célio7-Aug-17 0:47
memberCélio7-Aug-17 0:47 
AnswerRe: It has Snooze mode? Pin
Lothar Perr11-Sep-17 6:02
memberLothar Perr11-Sep-17 6:02 
QuestionHow To Use It , Demo to start some scheduled application Pin
Sushil.Agarwal11-May-17 23:27
memberSushil.Agarwal11-May-17 23:27 
QuestionCall another application from this one Pin
Venkat Raghvan18-Jul-16 22:33
memberVenkat Raghvan18-Jul-16 22:33 
Questionschadule batch Pin
ahmed mohmed21-Feb-16 22:21
memberahmed mohmed21-Feb-16 22:21 
BugError Pin
IndrajitDasgupat1-Dec-15 18:40
memberIndrajitDasgupat1-Dec-15 18:40 
QuestionManage Trigger Pin
Member 1185675929-Nov-15 3:22
memberMember 1185675929-Nov-15 3:22 
SuggestionTypo in MonthOfTheYear Enum Pin
chrisbray20-Apr-15 23:36
memberchrisbray20-Apr-15 23:36 
GeneralRe: Typo in MonthOfTheYear Enum Pin
Lothar Perr22-Apr-15 0:45
memberLothar Perr22-Apr-15 0:45 
SuggestionTake a look at events Pin
Rene Balvert20-Apr-15 3:21
memberRene Balvert20-Apr-15 3:21 
GeneralRe: Take a look at events Pin
Lothar Perr20-Apr-15 7:07
memberLothar Perr20-Apr-15 7:07 
GeneralRe: Take a look at events Pin
Rene Balvert20-Apr-15 7:18
memberRene Balvert20-Apr-15 7:18 
GeneralRe: Take a look at events Pin
Lothar Perr20-Apr-15 7:57
memberLothar Perr20-Apr-15 7:57 
QuestionUTC Pin
Wombaticus19-Apr-15 4:29
memberWombaticus19-Apr-15 4:29 
AnswerRe: UTC Pin
Lothar Perr19-Apr-15 8:09
memberLothar Perr19-Apr-15 8:09 
GeneralRe: UTC Pin
Wombaticus19-Apr-15 8:21
memberWombaticus19-Apr-15 8:21 
QuestionRequire task scheduler software Pin
Member 114314875-Feb-15 18:54
memberMember 114314875-Feb-15 18:54 
QuestionI could not download the source code Pin
dotnetmurthy5-Jan-15 22:42
memberdotnetmurthy5-Jan-15 22:42 
QuestionNot Triggering the Event :S Pin
UngarMax014-Dec-13 11:07
memberUngarMax014-Dec-13 11:07 
Questionhow to integrate window services with this application.!! Pin
Varun_nayak22-Nov-13 22:18
memberVarun_nayak22-Nov-13 22:18 
AnswerRe: how to integrate window services with this application.!! Pin
Lothar Perr19-Apr-15 8:33
memberLothar Perr19-Apr-15 8:33 
GeneralRe: how to integrate window services with this application.!! Pin
Varun_nayak29-Mar-16 18:41
memberVarun_nayak29-Mar-16 18:41 
Questionhelp me!How to create a task in Windows Xp,using Microsoft.Win32.TaskScheduler.dll. Pin
Member 1041057420-Nov-13 20:52
memberMember 1041057420-Nov-13 20:52 
BugWon't run as Windows Service Pin
rix_27-Sep-13 6:21
memberrix_27-Sep-13 6:21 
GeneralRe: Won't run as Windows Service Pin
Varun_nayak22-Nov-13 22:37
memberVarun_nayak22-Nov-13 22:37 
GeneralMy vote of 5 Pin
myeric12-Aug-13 21:33
membermyeric12-Aug-13 21:33 
Questionif on time run task system off after system do on are run task ??? Pin
ali21232111224-Jul-13 0:43
memberali21232111224-Jul-13 0:43 
AnswerRe: if on time run task system off after system do on are run task ??? Pin
Lothar Perr24-Jul-13 1:06
memberLothar Perr24-Jul-13 1:06 
GeneralRe: if on time run task system off after system do on are run task ??? Pin
Wombaticus19-Apr-15 4:25
memberWombaticus19-Apr-15 4:25 
GeneralRe: if on time run task system off after system do on are run task ??? Pin
Lothar Perr19-Apr-15 8:20
memberLothar Perr19-Apr-15 8:20 
QuestionHow to schedule trigger even if application terminated like window task schedule Pin
Varun_nayak19-Jul-13 22:31
memberVarun_nayak19-Jul-13 22:31 
AnswerRe: How to schedule trigger even if application terminated like window task schedule Pin
Lothar Perr24-Jul-13 1:03
memberLothar Perr24-Jul-13 1:03 
GeneralRe: How to schedule trigger even if application terminated like window task schedule Pin
Varun_nayak22-Nov-13 22:22
memberVarun_nayak22-Nov-13 22:22 
QuestionTriggerOneTimeOnly bug?? Pin
thomas3.1423-Apr-13 12:50
memberthomas3.1423-Apr-13 12:50 
QuestionHelp Pin
Bill Rady23-Apr-13 7:36
memberBill Rady23-Apr-13 7:36 
GeneralMy vote of 4 Pin
Prasad Khandekar17-Apr-13 9:23
memberPrasad Khandekar17-Apr-13 9:23 
QuestionUse the static method TaskScheduler.TriggerItem.FromXML??? Pin
Member 949456625-Mar-13 11:44
memberMember 949456625-Mar-13 11:44 
GeneralThanks For This Perfect Article Pin
sagar savsani5-Jan-13 20:34
membersagar savsani5-Jan-13 20:34 
QuestionFromXML Pin
wwwwe13-Nov-12 6:58
memberwwwwe13-Nov-12 6:58 
AnswerRe: FromXML Pin
Lothar Perr13-Nov-12 19:46
memberLothar Perr13-Nov-12 19:46 
GeneralRe: FromXML Pin
wwwwe14-Nov-12 3:40
memberwwwwe14-Nov-12 3:40 
GeneralRe: FromXML Pin
Lothar Perr6-Dec-12 9:04
memberLothar Perr6-Dec-12 9:04 
Questiontriggering exe Pin
markykoyski12-Nov-12 17:46
membermarkykoyski12-Nov-12 17:46 
AnswerRe: triggering exe Pin
Lothar Perr13-Nov-12 19:55
memberLothar Perr13-Nov-12 19:55 
GeneralGreat Job Pin
otakucrono20-Jul-10 12:20
memberotakucrono20-Jul-10 12:20 
GeneralRe: Great Job Pin
Lothar Perr27-Jul-10 21:37
memberLothar Perr27-Jul-10 21:37 
GeneralRe: Great Job Pin
vbytesdc3-Mar-11 9:48
membervbytesdc3-Mar-11 9:48 
GeneralNice work ..plz help for my struggle Pin
skyramesh22-Jan-10 23:03
memberskyramesh22-Jan-10 23:03 
GeneralRe: Nice work ..plz help for my struggle Pin
Lothar Perr3-Feb-10 5:25
memberLothar Perr3-Feb-10 5:25 
GeneralReally Nice article... Pin
Kushagra Tiwari7-Sep-09 2:49
memberKushagra Tiwari7-Sep-09 2:49 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 18 Apr 2015
Article Copyright 2009 by Lothar Perr
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid