我正在使用Quartz
库来运行计划任务。我像这样使用它
ISchedulerFactory scheduleFact = new StdSchedulerFactory();
IScheduler scheduler = scheduleFact.GetScheduler();
scheduler.Start();
IJobDetail job = JobBuilder.Create<MyJob>()
.WithIdentity("myjob", "group 1")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("mytrigger", "group 1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(60*60*12)
.RepeatForever())
.Build();
scheduler.ScheduleJob(job, trigger);
这很好用。这意味着时间表每12小时运行一次。但我想要的是每天运行任务时间为13.00。我可以这样做。请帮助。
答案 0 :(得分:4)
var trigger = TriggerBuilder.Create()
.WithDailyTimeIntervalSchedule(s => s
.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(13, 00)))
.EndingDailyAfterCount(1))
.Build();
Tipp:在控制台上输出接下来的10次运行以进行调试。
var times = TriggerUtils.ComputeFireTimes(trigger as IOperableTrigger, null, 10);
foreach (var time in times) Console.WriteLine(time);
答案 1 :(得分:0)
您可以使用WithSchedule
创建作业并使用CronScheduleBuilder
来定义作业频率 -
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("mytrigger", "group1")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(13, 00))
.ForJob(myJobKey)
.Build();
或者,您也可以在使用WithCronSchedule
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("mytrigger", "group1")
.WithCronSchedule("0 00 13 * * ?")
.ForJob("myJob", "group1")
.Build();
可以找到更多详细信息here。