I've been trying to figure out how to call the ScheduleJobs methods within Quartz.Net, but struggling to create the correct parameters it's expecting.
Here's what I've tried:
IJobDetail jobDetail = JobBuilder.Create<ReportJob>()
.WithIdentity("theJob")
.Build();
ITrigger everydayTrigger = TriggerBuilder.Create()
.WithIdentity("everydayTrigger")
// fires
.WithCronSchedule("0 0 12 1/1 * ?")
// start immediately
.StartAt(DateBuilder.DateOf(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year))
.Build();
ITrigger yearlyTrigger = TriggerBuilder.Create()
.WithIdentity("yearlyTrigger")
// fires
.WithCronSchedule("0 0 12 1 1 ? *")
// start immediately
.StartAt(DateBuilder.DateOf(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year))
.Build();
var dictionary = new Dictionary<IJobDetail, ISet<ITrigger>>();
dictionary.Add(jobDetail, new HashSet<ITrigger>() { everydayTrigger, yearlyTrigger });
sched.ScheduleJobs((dictionary, true); // is a cast possible here to IDictionary?
Other attempts;
sched.ScheduleJobs(new [] { jobDetail, new[] { afternoonTrigger } }, true);
&&
sched.ScheduleJobs(new[] { new KeyValuePair<IJobDetail, ITrigger>()}.ToDictionary<IJobDetail, Quartz.Collection.ISet<ITrigger>(), true);
Make sure you use the collections from Quartz's namespace, here's a version of your code that compiles with Quartz 2.x:
IJobDetail jobDetail = JobBuilder.Create<ReportJob>()
.WithIdentity("theJob")
.Build();
ITrigger everydayTrigger = TriggerBuilder.Create()
.WithIdentity("everydayTrigger")
// fires
.WithCronSchedule("0 0 12 1/1 * ?")
// start immediately
.StartAt(DateBuilder.DateOf(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year))
.Build();
ITrigger yearlyTrigger = TriggerBuilder.Create()
.WithIdentity("yearlyTrigger")
// fires
.WithCronSchedule("0 0 12 1 1 ? *")
// start immediately
.StartAt(DateBuilder.DateOf(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year))
.Build();
var dictionary = new Dictionary<IJobDetail, Quartz.Collection.ISet<ITrigger>>();
dictionary.Add(jobDetail, new Quartz.Collection.HashSet<ITrigger>()
{
everydayTrigger,
yearlyTrigger
});
sched.ScheduleJobs(dictionary, true);
Please not that for Quartz 3 you should use the BCL types, like System.Collections.Generic.HashSet<T>
, there's no need to use Quartz's custom types anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With