Click here to Skip to main content
15,867,330 members
Articles / All Topics

Scheduling With Quartz.Net

Rate me:
Please Sign up or sign in to vote.
4.69/5 (9 votes)
5 Jan 2015CPOL1 min read 75.1K   13   8
How to do scheduling with Quartz.Net

The other day, I had a requirement to schedule something in my app to run at certain times, and at fixed intervals thereafter. Typically, I would just solve this using either a simple Timer, or turn to my friend Reactive Extensions by way of Observable.Timer(..).

The thing is that I decided to have a quick look at something I have always known about but never really used, for scheduling, which is Quartz.net, which actually does have some pretty good documentation up already:

For me, I just wanted to get something very basic up and running, so I gave it a blast.

Step 1: Install Quartz.net

This is as easy as installing the following NuGet package “Quartz“.

Step 2: Create A Job Class

This again is fairly easy, thanks to Quartz nice API. Here is my job class

C#
using System;
using Quartz;
 
namespace FooBar
{
    public class LoggingJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
 
 
            Common.Logging.LogManager.Adapter.GetLogger("LoggingJob").Info(
                string.Format("Logging job : {0} {1}, and proceeding to log", 
                    DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
 
        }
    }
}

That is all you need for a job really. The context object gives you access to a lot of useful stuff.

Step 3: Setting Up A Schedule

Again, this was mega easy, all I had to do was something like this:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using Common.Logging;
 
using Quartz;
using Quartz.Impl;
 
namespace FooBar
{
    class Program
    {
 
        private static ILog Log = LogManager.GetCurrentClassLogger();
 
        static void Main(string[] args)
        {
            try
            {
                // construct a scheduler factory
                ISchedulerFactory schedFact = new StdSchedulerFactory();
 
                // get a scheduler
                IScheduler sched = schedFact.GetScheduler();
                sched.Start();
 
                IJobDetail job = JobBuilder.Create<LoggingJob>()
                    .WithIdentity("myJob", "group1")
                    .Build();
 
                ITrigger trigger = TriggerBuilder.Create()
                    .WithDailyTimeIntervalSchedule
                    (s =>
                        s.WithIntervalInSeconds(10)
                            .OnEveryDay()
                            .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(10, 15))
                    )
                    .Build();
 
                sched.ScheduleJob(job, trigger);
            }
            catch (ArgumentException e)
            {
                Log.Error(e);
            } 
        }
    }
}

And that was enough for my job to get scheduled, every 10 seconds starting at 10:15 AM.

I was fairly happy with Quartz, and I will certainly make more use of it, when I have bigger, bolder, badder scheduling needs.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
QuestionCreate Simple Task Scheduler desktop application in c# Pin
saylibh6-Mar-19 20:35
saylibh6-Mar-19 20:35 
QuestionA UI for scheduling jobs? Pin
ZurdoDev10-Mar-15 7:57
professionalZurdoDev10-Mar-15 7:57 
AnswerRe: A UI for scheduling jobs? Pin
Sacha Barber10-Mar-15 11:03
Sacha Barber10-Mar-15 11:03 
Questionblocking ui? Pin
Rahman Mahmoodi14-Jan-15 20:17
Rahman Mahmoodi14-Jan-15 20:17 
AnswerRe: blocking ui? Pin
Sacha Barber14-Jan-15 21:50
Sacha Barber14-Jan-15 21:50 
I would certainly hope so
GeneralMy vote of 5 Pin
Rajesh Buddaraju13-Jan-15 1:39
Rajesh Buddaraju13-Jan-15 1:39 
GeneralMy vote of 5 Pin
skyslogd7-Jan-15 10:07
skyslogd7-Jan-15 10:07 
GeneralRe: My vote of 5 Pin
Sacha Barber8-Jan-15 3:24
Sacha Barber8-Jan-15 3:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.