Flexible, lightweight CRON compliant scheduler. Works directly from the code. Main features:

  • Compatible with most of what CRON is offering
  • Setting the CRON is as simple as setting one string. Example bellow.
  • It also supports simple intervals in addition to CRON style schedules
  • Schedule can have valid from / to limits
  • OnSchedule event can be called in a new background thread or in context of main thread
  • Very lightweight implementation
CRON Scheduler download: Version 2.2.9 (28.01.2014)

The demo bellow shows the basic usage of CRON Scheduler. If you have trouble using the code or have any other questions, please contact me directly using the “Contact” page.

Warning: My version of CRON implementation also uses seconds so it actually has one more format option. You must be aware of that when you write your CRON string. The format is like that:

<Second> <Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week>

Sample scheduler usage:

procedure TfMain.FormCreate(Sender: TObject);
var
  NewSchedule: TScheduledEvent;
begin
  // clear memo
  lbEvents.Clear;
  // crete new event list that will hold events
  EventList := TSchEventList.Create;
  // first event
  NewSchedule := EventList.Add('ScheduleOne');
  NewSchedule.Schedule.EventPlan := '0 */2 * * * * *';
  NewSchedule.OnScheduleEvent := OnScheduleOneTrigger;
  NewSchedule.Run;
  // second event
  NewSchedule := EventList.Add('ScheduleTwo');
  NewSchedule.Schedule.EventPlan := '*/30 * * * * * *';
  NewSchedule.OnScheduleEvent := OnScheduleTwoTrigger;
  NewSchedule.Run;
  // third event
  NewSchedule := EventList.Add('ScheduleThree');
  NewSchedule.Schedule.EventPlan := '*/5 * * * * * *';
  NewSchedule.OnScheduleEvent := OnScheduleThreeTrigger;
  NewSchedule.Run;
  // fourth event
  NewSchedule := EventList.Add('ScheduleFour');
  NewSchedule.Schedule.EventPlan := '15 * * * * * *';
  NewSchedule.OnScheduleEvent := OnScheduleFourTrigger;
  NewSchedule.Run;
  // start stop dynamic event
  FDynamicEvent := EventList.Add('DynamicSchedule');
  FDynamicEvent.Schedule.EventPlan := '*/5 * * * * * *';
  FDynamicEvent.OnScheduleEvent := OnDynamicScheduleTrigger;
 
  Caption := Format('Started at : %s ( Press ''d'' for dynamic event )', [DateTimeToStr(Now)]);
end;

Event handlers:

procedure TfMain.OnDynamicScheduleTrigger(Sender: TScheduledEvent);
begin
  lbEvents.Items.Add(Format('Dynamic event was trigered at : %s', [DateTimeToStr(now)]));
end;
 
procedure TfMain.OnScheduleFourTrigger(Sender: TScheduledEvent);
begin
  lbEvents.Items.Add(Format('Event four was trigered at : %s', [DateTimeToStr(now)]));
end;
 
procedure TfMain.OnScheduleOneTrigger(Sender: TScheduledEvent);
begin
  lbEvents.Items.Add(Format('Event one was trigered at : %s', [DateTimeToStr(now)]));
end;

Example how to use From / To valid range. The event will fire for one year, every sunday, every second hour, but only on 1,5 and 10 month in the year.

NewSchedule := EventList.Add('ScheduleOne');
NewSchedule.Schedule.EventPlan := '0 0 */2 * 1,5,10 7 *';
NewSchedule.OnScheduleEvent := OnScheduleOneTrigger;
NewSchedule.Schedule.ValidFrom := EncodeDate(2010, 1, 1);
NewSchedule.Schedule.ValidTo := EncodeDate(2010, 12, 31);
NewSchedule.Run;

Example how to fire an event at an 2 hour interval from teh start of the schedule. Each event will be fired in a new thread.

NewSchedule := EventList.Add('ScheduleOne');
NewSchedule.Schedule.SetInterval(0, 0, 2, 0, 0, 0);
NewSchedule.OnScheduleEvent := OnScheduleOneTrigger;
NewSchedule.SignalType := stThreaded;
NewSchedule.Run;

Change Log

  • 2.2.9
    • If RunAtOnce is specified and ValidFrom is valid date then execute then
  • 2.2.8
    • stQueue added as signal type
    • In schedule destructor call stop
    • Stop the schedule thread in a nicer way
  • 2.2.7
  • 2.2.6
  • 2.2.5
    • LastEvent property for schedule
  • 2.2.4
    • Terminate event is now created only when schedule is run
  • 2.2.4
    • Terminate event is now created only when schedule is run
  • 2.2.3
    • Improved cron format support. Now “1,3,2,5-9,15-20/2,5,1,18-25/3″ gets parsed correctly
    • Sort numbers on the value list to get correct sequence
    • Eliminate duplicate values
    • Additional format checks

  • 2.2.2
    • ExecuteLimit added. Allows the event to be executed only N times
  • 2.2.1
    • Use QueryPerformanceCounter instead of GetTickCount (thanks Roberto Neto)
    • Code adaptation for compatibility with Delphi 7
  • 2.2.0
    • Fixed a bug with day of the week not having a correct impact on month
    • Raise exception if the schedule is not in the correct format (a start)
    • Made FPC compatible (initial implementation)
  • 2.1.2
    • Fixed a bug when deleting a schedule from the list (it was not removed). Thanks to Daniel for pointing that out.
  • 2.1.1
    • Fixed a bug in calculation algorithm
  • 2.1.0
    • 64 bit compiler compatible
  • 2.0.3
    • added HasValue function for TChronEntry
  • 2.0.2
    • fixed a bug in valid interval calculation