Click here to Skip to main content
15,886,518 members
Articles / Web Development / ASP.NET
Article

Event Based Scheduler

Rate me:
Please Sign up or sign in to vote.
2.71/5 (6 votes)
1 Jun 2007CPOL1 min read 33.9K   685   33   1
A periodic scheduler, based on the timer control

Introduction

This article aims to solve the scheduling problem within an application.

We all know the simple scenario where we need to write something which needs to run every X time. The solution in this case is easy – Timer. You give it the iteration time (let's say 5 minutes), set up the event handler so every 5 minutes it will fire and you are done.

The case gets trickier when you are asked to run your application every X time starting from Y. In this case, you need someone/something to know when to start (usually less than X time from now) and then to keep track of the iterations.

Background

My need for this solution came from a need to run a Windows service which will do some DB executions every 24 hours at 03:00. The new application came to replace a SQL-server job which was basically doing the same thing, only this time we had to run the application elsewhere.

Using the Code

The use is very easy and looks very similar to the use we usually have with the Timer control:

C#
DateTime start = System.DateTime.Now.AddMinutes(1);
Scheduler.PeriodicSchedules scheduler =
	new Scheduler.PeriodicSchedules
	(start,Scheduler.PeriodicSchedules.ExecutionType.Minutely);
scheduler.Elapsed+=new System.Timers.ElapsedEventHandler(scheduler_Elapsed);
scheduler.Enabled = true; 

As you can see, the PeriodicSchedules constructor gets a DateTime object representing the time it should run the first time and ExecutionType enumerator representing the interval.

The enumerator can be extended easily to any interval you might want.

Points of Interest

I was using the ICurrentTime interface in order to enable unit tests for the application. This way, I can inject the CurrentTime of the machine from the outside instead of playing with the local clock in order to test different scenarios:

C#
public interface ICurrentTime
{
    DateTime GetCurrentDateTime();
}

And while testing:

C#
public class CurrentTime:ICurrentTime
{        
    DateTime currentTime;
    public CurrentTime(DateTime mynow)
    {
        currentTime = mynow;
    }
    public DateTime GetCurrentDateTime()
    {
        return currentTime;
    }        
}
[Test]
public void OneHourEarlierInterval()
{
    CurrentTime now = new CurrentTime(new DateTime(2007, 5, 30, 15, 0, 0));

    Scheduler.PeriodicSchedules scheduler =
	new Scheduler.PeriodicSchedules(14, 0, 0, 
	Scheduler.PeriodicSchedules.ExecutionType.Hourly, now);
    Console.WriteLine(scheduler.Interval.ToString());

    Assert.AreEqual((double)(60000 * 60), scheduler.Interval);
}

History

  • 2nd June, 2007: Initial post

License

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


Written By
Architect
Israel Israel
Working for the last 10 years as a developer / team leader.

Getting more and more into automated unit tests & architecture design.

Now days working with various Microsoft products - BizTalk, Share Point, MCMS and C# in general.

Comments and Discussions

 
GeneralSchedule stops running after a 2-3 days Pin
Mike Lucas18-Jan-11 12:18
Mike Lucas18-Jan-11 12:18 

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.