C#:如何通过 Quartz.net 在一天的不同时间安排多个例程一次 [英] C#:How to schedule multiple routine by Quartz.net at different time of the day once

查看:150
本文介绍了C#:如何通过 Quartz.net 在一天的不同时间安排多个例程一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何每天在特定时间启动我的一个例程.这是代码.

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.

假设routine1应该在08:00启动,routine2应该在15:00启动并且routine2应该在18:00启动

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

推荐答案

就像 stuartd 所说的,你的工作(例程 2)需要多个触发器.我还建议使用 CronTrigger 而不是 SimpleTrigger.您可以使用以下命令轻松创建 CronTrigger:

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();

然后只需使用 2 个触发器安排您的工作:

And then just schedule your job with the 2 triggers:

sched.ScheduleJob(job, trigger1);
sched.ScheduleJob(job, trigger2);

这篇关于C#:如何通过 Quartz.net 在一天的不同时间安排多个例程一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆