Create a recurring schedule for a job

Languages: C#, VB.NET

View on GitHub to download or comment.

See the Client API Examples Introduction for additional information and prerequisites.

Samples index

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.

sample.cs
//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);
sample.vb
'create the DateCriterion to specify the days when the job should run
Dim dateCriterion = TryCast(context.CreateObject(ClassID.DateCriterion), DateCriterion)

'specify that the job should run only on specified days: Monday to Friday
dateCriterion.CriterionType = DateCriterionType.SpecifiedDays
Dim rule = TryCast(context.CreateObject(ClassID.DaySpecification), 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
Dim timeCriterion = TryCast(context.CreateObject(ClassID.TimeCriterion), TimeCriterion)

'run every 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
Dim schedule = TryCast(context.CreateObject(ClassID.Schedule), Schedule)
schedule.TimeCriterion = timeCriterion
schedule.DateCriterion = dateCriterion

'create a ScheduleTrigger for the job
Dim trigger = TryCast(context.CreateObject(ClassID.ScheduleTrigger), ScheduleTrigger)
trigger.Schedules.Add(schedule)
job.Triggers.Add(trigger)

Comments

View on GitHub to comment.