Click here to Skip to main content
Licence CPOL
First Posted 1 Jun 2007
Views 16,623
Downloads 362
Bookmarked 33 times

Event Based Scheduler

By | 1 Jun 2007 | Article
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:

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:

public interface ICurrentTime
{
    DateTime GetCurrentDateTime();
}

And while testing:

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)

About the Author

Doron Goldberg

Architect

Israel Israel

Member

Follow on Twitter Follow on Twitter
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSchedule stops running after a 2-3 days PinmemberMike Lucas12:18 18 Jan '11  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 2 Jun 2007
Article Copyright 2007 by Doron Goldberg
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid