Click here to Skip to main content
15,179,830 members
Articles / Programming Languages / C#
Posted 30 Sep 2004


211 bookmarked

A component for event scheduling inside an application

Rate me:
Please Sign up or sign in to vote.
4.89/5 (67 votes)
30 Sep 20043 min read
This article presents the design and a readily usable component for scheduling events which are consumed inside a server or service application.


Every typical server or service application need scheduling of some events inside the application. These events generally are supposed to wake up at certain determined times to do self checks, check statuses of threads, or typically refresh resources once in a while, and so on. This requires shorter and long time scheduling of events inside the application. The most common approach is to use timers for different tasks and attach them to threads. A scheduler component is presented in this article for simplifying a way to create and maintain event scheduling inside an application.

Using the code

The best example to create and use the Schedule classes is in the demo application. This piece of code inside the demo creates all the types of schedule objects implemented in the library, and also provides a delegate ScheduleCallBack() for the schedule to call back for OnTrigger event.

// create and add different types of schedules
Schedule s = new IntervalSchedule("Test_Interval", 
             DateTime.Now.AddMinutes(1), 45, TimeSpan.Zero, 
             new TimeSpan(TimeSpan.TicksPerDay));
s.OnTrigger += new EventScheduler.Invoke(ScheduleCallBack);

s = new OneTimeSchedule("Test_Onetime", DateTime.Now.AddMinutes(1.5));
s.OnTrigger += new EventScheduler.Invoke(ScheduleCallBack);

s = new DailySchedule("Test_daily", DateTime.Now.AddMinutes(2));
s.OnTrigger += new EventScheduler.Invoke(ScheduleCallBack);

s = new WeeklySchedule("Test_weekly", DateTime.Now.AddMinutes(2.5));
s.OnTrigger += new EventScheduler.Invoke(ScheduleCallBack);

s = new MonthlySchedule("Test_monthly", DateTime.Now.AddMinutes(3));
s.OnTrigger += new EventScheduler.Invoke(ScheduleCallBack);

As can be seen, the three main lines are to:

  • create a Schedule instance
  • subscribe to the OnTrigger event, and
  • add the Schedule to the Scheduler's list

That's it! To see this in action, run the demo application shown below and click on Test Code button. This will create an instance of each schedule and gives a feeling of what the scheduler can do.

Image 1

Design and Code details

The following class diagram shows the different classes in the scheduler library and their relationship.

Image 2

At the root of the library is the Schedule class which implements IComparable interface. The IComparable interface provides the CompareTo(Object) method which is used to compare two Schedule objects for sorting the list of schedules to determine the sequence of invocation times for the timer.

The library already provides general Schedules like OneTimeSchedule (used for raising an event only once), IntervalSchedule (used to raise an event at regular intervals), DailySchedule, WeeklySchedule and MonthlySchedule, which are self explanatory. The Schedule base class has generic properties like Name, Type, NextInvokeTime etc. used by all derived objects, and some specific properties like Interval which is used in this case only by IntervalSchedule.

These are the steps through which the Scheduler goes through for scheduling an event:

  • A static timer (Scheduler.Timer) with a call back method (DispatchEvents) is created and initially put to sleep.
  • Schedules are created either by code or by GUI. Individual conditions are checked in the Schedule's constructor.
  • When ever a Schedule object is added to the Scheduler using AddSchedule(Schedule s) method:
    • the list is sorted. The list which is an ArrayList in turn uses the Schedule.CompareTo(Object) method to determine the sorting order.
    • the NextInvokeTime of the first element in the list is used by the Timer to decide when to wake up next.
  • When the Timer wakes up:
    • it calls DispatchEvents call back
    • which calls TriggerEvents() on the first Schedule object in the list

Image 3

A Schedule's view GUI is also provided with the library to manage the schedules through a GUI as a singleton class (ScheduleUI) which is created by the static method ScheduleUI.ShowSchedules(). This GUI uses the Scheduler.OnSchedulerEvent event provided by Scheduler to track when a Schedule is created, deleted, or invoked.

Image 4

Typically, this GUI can be used for administration of schedules and also to create or delete them easily. Once a schedule is created, it can be accessed programmatically using Scheduler events or the name of the Schedule itself.

For e.g., the following code inside ScheduleUI class shows how the Scheduler events are used to refresh the list of Schedules.

public void OnSchedulerEvent(SchedulerEventType type, string scheduleName)
        case SchedulerEventType.CREATED:
            ListViewItem lv = SchedulesView.Items.Add(scheduleName);
            Schedule s = Scheduler.GetSchedule(scheduleName);
            lv.SubItems.Add(s.NextInvokeTime.ToString("MM/dd/yyyy hh:mm:ss tt"));
        case SchedulerEventType.DELETED:
            for (int i=0; i<SchedulesView.Items.Count; i++)
                if (SchedulesView.Items[i].Text == scheduleName)
        case SchedulerEventType.INVOKED:
            for (int i=0; i<SchedulesView.Items.Count; i++)
                if (SchedulesView.Items[i].Text == scheduleName)
                    Schedule si = Scheduler.GetSchedule(scheduleName);
                    SchedulesView.Items[i].SubItems[2].Text =
                        si.NextInvokeTime.ToString("MM/dd/yyyy hh:mm:ss tt");

Another critical piece of code is the bool[] m_workingWeekDays array in the Schedule base class, in which the active week days are stored. The bool NoFreeWeekDay() and bool CanInvokeOnNextWeekDay() use this array to determine if a schedule can run on a week day.

Similarly, the bool IsInvokeTimeInTimeRange() determines if a schedule can run in a time range on any given day. Please refer to comments in the code if you are interested to get into the details.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Sriram Chitturi
United States United States
No Biography provided

Comments and Discussions

QuestionSave schedules and loading at run time Pin
wtoast10-Dec-14 11:40
Memberwtoast10-Dec-14 11:40 
QuestionOne little bug and one inconsistency Pin
Member 797473426-May-14 12:09
MemberMember 797473426-May-14 12:09 
QuestionLicence?? Pin
pippo pioppo30-Aug-12 4:24
Memberpippo pioppo30-Aug-12 4:24 
Questionquery Pin
Apurva Kunkulol9-May-12 22:41
professionalApurva Kunkulol9-May-12 22:41 
GeneralMy vote of 3 Pin
programmerdon22-Mar-12 10:18
Memberprogrammerdon22-Mar-12 10:18 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey12-Feb-12 21:37
professionalManoj Kumar Choubey12-Feb-12 21:37 
Generaltake a look at Quartz Pin
Chris Tracy17-May-08 10:30
MemberChris Tracy17-May-08 10:30 
GeneralRe: take a look at Quartz Pin
J0J0_28-Jun-08 4:18
MemberJ0J0_28-Jun-08 4:18 
GeneralVS 2008 problem: Cross-thread operation not valid Pin
Mr_AndersonNET6-May-08 0:14
MemberMr_AndersonNET6-May-08 0:14 
GeneralRe: VS 2008 problem: Cross-thread operation not valid Pin
Peeter15-Jan-10 10:59
MemberPeeter15-Jan-10 10:59 
AnswerRe: VS 2008 problem: Cross-thread operation not valid Pin
Prabakar Samiyappan20-Feb-11 8:23
professionalPrabakar Samiyappan20-Feb-11 8:23 
GeneralRe: VS 2008 problem: Cross-thread operation not valid Pin
JonFrost18-Mar-16 0:36
MemberJonFrost18-Mar-16 0:36 
Questionnew version for schedle? Pin
qq3444863510-Apr-08 3:59
Memberqq3444863510-Apr-08 3:59 
QuestionPersist in a database Pin
camlopes21-Jan-08 5:33
Membercamlopes21-Jan-08 5:33 
GeneralThanks for the idea Pin
Joseph Wee27-Nov-07 13:57
MemberJoseph Wee27-Nov-07 13:57 
GeneralRe: Thanks for the idea Pin
Sriram Chitturi28-Nov-07 1:06
MemberSriram Chitturi28-Nov-07 1:06 
GeneralRunning without GUI Pin
roseen25-Sep-07 22:51
Memberroseen25-Sep-07 22:51 
GeneralRe: Running without GUI Pin
samphil26-Sep-07 3:42
Membersamphil26-Sep-07 3:42 
GeneralNew version Pin
dima polyakov21-Sep-07 11:16
Memberdima polyakov21-Sep-07 11:16 
GeneralRe: New version Pin
ingos200722-Sep-07 12:23
Memberingos200722-Sep-07 12:23 
GeneralRe: New version Pin
dima polyakov23-Sep-07 11:56
Memberdima polyakov23-Sep-07 11:56 
GeneralRe: New version Pin
samphil26-Sep-07 3:40
Membersamphil26-Sep-07 3:40 
GeneralRe: New version Pin
dima polyakov26-Sep-07 6:07
Memberdima polyakov26-Sep-07 6:07 
GeneralPerhaps you should read up on 'lock' Pin
robvon19-Sep-07 0:20
Memberrobvon19-Sep-07 0:20 
GeneralFIX - Intervals were not expiring. Pin
r3b123-Aug-07 12:22
Memberr3b123-Aug-07 12:22 

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.