Table of Contents

Create a recurring schedule for a job

This sample uses the adTempus API to create a recurring schedule for a job.

This code assumes you have already created the job and have a DataContext names context and a Job named job.

It creates a Schedule that executes every hour from 5am to 9pm, Monday through Friday.

//create the DateCriterion to specify the days when the job should run
var dateCriterion=context.CreateObject(ClassID.DateCriterion) as DateCriterion;

//specify that the job should run only on specified days: Monday to Friday
dateCriterion.CriterionType = DateCriterionType.SpecifiedDays;
var rule=context.CreateObject(ClassID.DaySpecification) as DaySpecification;
rule.SetWeekday(Weekday.Monday,true);
rule.SetWeekday(Weekday.Tuesday,true);
rule.SetWeekday(Weekday.Wednesday,true);
rule.SetWeekday(Weekday.Thursday,true);
rule.SetWeekday(Weekday.Friday,true);			
dateCriterion.Days.Add(rule);

//create the TimeCriterion to specify the times the job should run on the specified days
var timeCriterion=context.CreateObject(ClassID.TimeCriterion) as TimeCriterion;

//run every 1 hour
timeCriterion.CriterionType= TimeCriterionType.Interval;
timeCriterion.Interval=1;	
timeCriterion.IntervalType=DayTimeInterval.Hour;

//run between 5am and 9pm
//if you want to run all day (12am to 11:59pm) you can omit the StartTime and EndTime
timeCriterion.StartTime = new DateTime(2000, 1, 1, 5, 0, 0); //set the start time to 5:00 am (the date part of the value is ignored).
timeCriterion.EndTime = new DateTime(2000, 1, 1, 21, 0, 0); //set the start time to 9:00pm (2100) (the date part of the value is ignored).

//create the schedule using the specified criteria
var schedule=context.CreateObject(ClassID.Schedule) as Schedule;
schedule.TimeCriterion=timeCriterion;
schedule.DateCriterion=dateCriterion;

//create a ScheduleTrigger for the job
var trigger=context.CreateObject(ClassID.ScheduleTrigger) as ScheduleTrigger;
trigger.Schedules.Add(schedule);

job.Triggers.Add(trigger);