Quartz.net:在特定的时间间隔内运行作业

时间:2015-01-28 06:30:57

标签: c# quartz.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;
using Quartz.Job;
using ConsoleApplication2;

namespace Lesson1
{
    class Program
    {
        static void Main(string[] args)
       {
            //Create the scheduler factory
            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

            //Ask the scheduler factory for a scheduler
            IScheduler scheduler = schedulerFactory.GetScheduler();

            //Start the scheduler so that it can start executing jobs
            scheduler.Start();

            // Create a job of Type WriteToConsoleJob
            IJobDetail job = JobBuilder.Create(typeof(WriteToConsoleJob)).Build();

            ITrigger trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(s => s.WithIntervalInMinutes(15).OnMondayThroughFriday().StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(9, 0))).Build();


            scheduler.ScheduleJob(job, trigger);


            //A nice way to stop the scheduler, waiting for jobs that are running to finish
            scheduler.Shutdown(true);
        }
    }
}

我已经创建了一个测试工作,并且工作日的工作正常,从0900开始在15分钟后重复,但我想在特定的时间间隔运行它,即0900到1500小时。 而且我不想为此使用CronTrigger。

1 个答案:

答案 0 :(得分:6)

添加EndingDailyAt来电:

ITrigger trigger = TriggerBuilder
            .Create()
            .WithDailyTimeIntervalSchedule(s =>  
                         s.WithIntervalInMinutes(15)
                          .OnMondayThroughFriday()
                          .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(9, 0))
                          .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(15, 0)))
           .Build();