C#:How to schedule multiple routine by Quartz.net

2019-09-20 09:56发布

This question already has an answer here:

i know how to fire my one routine every day at specific time of day. here is the code.

IScheduler sched = null;
//construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();

//get a scheduler
sched = schedFact.GetScheduler();
sched.Start();

IJobDetail job = JobBuilder.Create<frmMain>()
.WithIdentity("Job", "group")
.Build();

ITrigger trigger = TriggerBuilder.Create()
.WithDailyTimeIntervalSchedule
 (s =>
    s.WithIntervalInHours(24)
   .OnEveryDay()
   .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(19, 07))
 )
.Build();

sched.ScheduleJob(job, trigger);

suppose now i am in scenario that i need to trigger many routine at different time of the day once.

say routine1 should fire at 08:00, routine2 should fire at 15:00 and routine2 should fire at 18:00

now give me suggestion how could i fire different routine at different time of the day. thanks

标签: c# quartz.net
1条回答
劳资没心,怎么记你
2楼-- · 2019-09-20 10:36

Like stuartd stated, you need multiple triggers for your job(routine2). I would also suggest to use CronTrigger instead of SimpleTrigger. You can easily create a CronTrigger with:

var trigger1 = TriggerBuilder.Create()
                .WithDescription(name)
                .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(15, 0))
                .Build();

var trigger2 = TriggerBuilder.Create()
                .WithDescription(name)
                .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(18, 0))
                .Build();

And then just schedule your job with the 2 triggers:

sched.ScheduleJob(job, trigger1);
sched.ScheduleJob(job, trigger2);
查看更多
登录 后发表回答