a blog for those who code

Wednesday 3 August 2016

Schedule Tasks as a Windows Service using Quartz.Net

In this post we will be discussing about scheduling tasks as a windows service using Quartz.Net. Scheduling regular tasks can be anything for example sending emails once every day, tweeting about anything etc.

This article is very basic implementation that will get you up and running with a schedules email job. The basic features of Quartz.Net includes Job Scheduling, Job Execution, Job Persistence and Clustering.

At first create a Windows Service Project as shown below, and then install Quartz.Net using Nuget Pacakge.


For scheduling a Job you need to know about 3 components, job, trigger and scheduler. A job is the task which needs to be performed (sending email, tweeting). Trigger is about scheduling that is how and when the job to be executed. Scheduler is nothing but a agent which takes care that job is performed on the schedule dictated by the trigger.

At first we will create a Job Class, which should implement IJob interface and which should have one Execute method as shown below :

using System;
using Quartz;
using Quartz.Impl;

namespace SchedulingTasks
{
  public class MyJob : IJob
  {
    public void Execute(IJobExecutionContext context)
    {
       // Send Email Or Tweeting
    }   
  }
}

In the above example we have a Class MyJob which has implemented IJob interface and have a method Execute taking IJobExecutionContext object as parameter. Context contains the configuration data about the job. Now inside the the execute method you can do anything which you want to schedule for example sending emails or tweeting.

Now the next thing is to set up a Trigger which will trigger the execute method of the Job Class.

public class JobScheduler
{
  public void Start()
  {
    IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
    scheduler.Start();

    IJobDetail job = JobBuilder.Create<MyJob>().Build();

    ITrigger trigger = TriggerBuilder.Create()
           .WithSimpleSchedule(a => a.WithIntervalInMinutes(15).RepeatForever())
           .Build();

    scheduler.ScheduleJob(job, trigger);
  }

  public void Stop()
  {
    IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
    scheduler.Shutdown();
  }
}

Then we will modify our Service.cs where we will start the scheduler when our service will start and stop it when our service will stop. Inside your Service1.cs class you can start and stop your service as shown below :

public partial class Service1 : ServiceBase
{
  JobScheduler scheduler;
  public Service1()
  {
    InitializeComponent();
  }
  protected override void OnStart(string[] args)
  {
    scheduler = new JobScheduler();
    scheduler.Start();
  }
  protected override void OnStop()
  {
    if (scheduler != null)
    {
      scheduler.Stop();
    }
  }
}

Now to Install the service you will type InstallUtil -i MyServiceName.exe


And then goto services.msc and start your installed service. To Uninstall the service you should run InstallUtil -u MyServiceName.exe


Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.

2 comments:

  1. Thank you for the tutorial. I just want to know if it works for a web farm enviroment, can I have the service installed in every server and achieve that the code inside the service is only executed once and prevents other services from execute the same code when it has already been executed?

    ReplyDelete