Click here to Skip to main content
13,193,650 members (59,438 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

29.7K views
13 bookmarked
Posted 5 Jan 2015

Scheduling With Quartz.Net

, 5 Jan 2015
Rate this:
Please Sign up or sign in to vote.
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

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:

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)

Share

About the Author

Sacha Barber
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

You may also be interested in...

Comments and Discussions

 
QuestionA UI for scheduling jobs? Pin
RyanDev10-Mar-15 7:57
mvpRyanDev10-Mar-15 7:57 
AnswerRe: A UI for scheduling jobs? Pin
Sacha Barber10-Mar-15 11:03
mvpSacha Barber10-Mar-15 11:03 
Questionblocking ui? Pin
Rahman Mahmoodi14-Jan-15 20:17
memberRahman Mahmoodi14-Jan-15 20:17 
AnswerRe: blocking ui? Pin
Sacha Barber14-Jan-15 21:50
mvpSacha Barber14-Jan-15 21:50 
GeneralMy vote of 5 Pin
Rajesh Varma Buddaraju13-Jan-15 1:39
memberRajesh Varma Buddaraju13-Jan-15 1:39 
GeneralMy vote of 5 Pin
black-out7-Jan-15 10:07
memberblack-out7-Jan-15 10:07 
GeneralRe: My vote of 5 Pin
Sacha Barber8-Jan-15 3:24
mvpSacha 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171018.2 | Last Updated 6 Jan 2015
Article Copyright 2015 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid