如何使用简单的计划创建每n周触发一次的计划?

时间:2015-03-31 09:30:51

标签: c# .net quartz-scheduler quartz.net

如何使用quartz.net简单计划创建每隔n周触发一次的计划?

1 个答案:

答案 0 :(得分:0)

好的东西就像这样可以解决问题

  IScheduler sched;
private void ConfigureScheduler()
{
 int nbweeks = 1;
 int n = 24*7*nbweeks;
 var schedFact = new StdSchedulerFactory();
 sched = schedFact.GetScheduler();
 var job = new JobDetailImpl("weeklyjob", null, typeof(Type));
 var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
 var trigger = TriggerBuilder.Create()
                             .StartNow()
                             .WithSimpleSchedule(x=>x.RepeatForever().WithIntervalInHours(n))
                             .Build();

 sched.ScheduleJob(job, trigger);
 sched.Start();
}

根据您的需要改变nbweeks,它将每n周运行一次

此解决方案将在每个月的第14天(第2周)触发一次,您可以根据需要更改第二周的日期

IScheduler schedd;
private void ConfigureScheduler()
{
    var schedFact = new StdSchedulerFactory();

    schedd = schedFact.GetScheduler();
    var job = new JobDetailImpl("weeklyJob", null, typeof(Type));
    var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    var cronScheduleBuilder = CronScheduleBuilder.MonthlyOnDayAndHourAndMinute(14, 12, 0);
    var trigger = TriggerBuilder.Create()
                                .StartNow()
                                .WithSchedule(cronScheduleBuilder)
                                .Build();

    sched.ScheduleJob(job, trigger);
    sched.Start();
}

<强>更新 此代码将在每个月的每个星期一触发

 IScheduler schedd;
private void ConfigureScheduler()
{
    var schedFact = new StdSchedulerFactory();

    schedd = schedFact.GetScheduler();
    var job = new JobDetailImpl("weeklyJob", null, typeof(Type));
    var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    var cronScheduleBuilder = CronScheduleBuilder.CronSchedule("0 0 12 ? 1/1 MON#2 *");
    var trigger = TriggerBuilder.Create()
                                .StartNow()
                                .WithSchedule(cronScheduleBuilder)
                                .Build();

    sched.ScheduleJob(job, trigger);
    sched.Start();
}