问题
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);
回答1:
Make sure you use the collections from Quartz's namespace, here's a version of your code that compiles:
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);
来源:https://stackoverflow.com/questions/20396550/calling-schedule-with-schedulejobs