AF.
AF.

Reputation: 53

Quartz.net: Run a job for specific interval of time

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

I have created a test job and its working fine for weekdays repeating after 15 minutes starting at 0900 hours but i want to run it for specific interval of time i.e. 0900 to 1500 hours. And i don't want to use CronTrigger for this.

Upvotes: 5

Views: 2289

Answers (1)

stuartd
stuartd

Reputation: 73303

Add an EndingDailyAt call:

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

Upvotes: 9

Related Questions