Task Scheduling Through Quartz In C#

Introduction

In this article, we are going to learn about task scheduling through QUARTZ Scheduler in C#. Here, we are going to make a simple Windows.Forms application and schedule a job in it.

What is QUARTZ?

Quartz.Net is a .NET port of the popular Java job scheduling framework. It's an open source job scheduling system that can be used from the smallest apps to the large-scale enterprise systems. The official website of Quartz.Net states: "Quartz.Net is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems."

Things to do (Setting things up)

  • Go to "Manage NuGet Package".
  • Search for Quartz.NET.
  • Install it in your desired project.

    C#

Now, you are ready to schedule the task through Quartz Scheduler.

Example Program

Let’s make a simple program to understand it through apractical example. Here, we are going to make a simple Windows.Forms application in C#. I assume that your project is ready for development.

Scenario

Here, we are building a Windows.Forms application in which the first form contains a DateTime picker and a Schedule button. When a user clicks on the button, a job is scheduled at the time he/she sets in DateTime picker. And, this scheduled job starts doing its work at the given time and date.

Make Form1 like this.

C#

And, handle the ClickEvent of the button just like this.

We made two classes to achieve this scheduling task. One is of JOB and the other class is of the scheduler.

Class Schedular.cs

  1. using Quartz;  
  2. using Quartz.Impl;  
  3. using System;  
  4.   
  5. namespace SchedularPrac  
  6. {  
  7.   public  class Scheduler  
  8.     {  
  9.         public  void Start(DateTime date)  
  10.   
  11.         {  
  12.   
  13.             IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();  
  14.             scheduler.Start();  
  15.             IJobDetail job = JobBuilder.Create<Job>().Build();  
  16.             ITrigger trigger = TriggerBuilder.Create()  
  17.              .WithIdentity("IDGJob""IDG")  
  18.                .StartAt(date)  
  19.                .WithPriority(1)  
  20.                .Build();  
  21.             scheduler.ScheduleJob(job, trigger);  
  22.   
  23.         }  
  24.   
  25.     }  
  26. }  

Explanation of class

This class will schedule the job written in the job.cs class at the given time and the job will start executing with high priority. When the given time comes, the scheduler will trigger the given job and start working on it.

Class Job.cs

  1. using Quartz;  
  2.   
  3. namespace SchedularPrac  
  4. {  
  5.     public class Job : IJob  
  6.     {  
  7.         public void Execute(IJobExecutionContext context)  
  8.         {  
  9.             Form2 fm = new Form2();  
  10.             fm.ShowDialog();  
  11.         }  
  12.     }  
  13. }  

Explanation

This is the job which we want to execute at given time. Here, just a new form is opened when the job starts.

Code of Form1

  1. public Form1()  
  2.         {  
  3.             InitializeComponent();  
  4.             dateTimePicker1.Format = DateTimePickerFormat.Custom;  
  5.             dateTimePicker1.CustomFormat = "MM/dd/yyyy hh:mm:ss";  
  6.         }  
  7.   
  8.         private void simpleButton1_Click(object sender, EventArgs e)  
  9.         {  
  10.            DateTime date =  dateTimePicker1.Value;  
  11.             Scheduler sc = new Scheduler();  
  12.             sc.Start(date);  
  13.         }  

Explanation

When the form is initialized, the date time picker format is changed and contains hours, minutes, and second along with the date so the user will start the scheduler at a more precise time. And, in click handler of a button, the Scheduler class is started with the given date.

Output

C#

Here, I have scheduled a job for 10 November 2017 11:00:16 and clicked on the Schedule button. After this job is scheduled, it starts waiting for time to start its execution. At the scheduled time, a new form is opened which contains current date-time and a status.

C#

Form2 Code

  1. public Form2()  
  2.         {  
  3.             InitializeComponent();  
  4.   &nbFsp;         label2.Text = DateTime.Now.ToString();  
  5.         }  

Label2 contains the current date and time.

C#

So, the reliable results come out at the end. And, you can see that this scheduler works nicely and executes its job perfectly at any given time.

Up Next
    Ebook Download
    View all
    Learn
    View all