using System;

using Quartz;

using Quartz.Impl;





namespace SchedulingTasks

{

public class MyJob : IJob

{

public void Execute(IJobExecutionContext context)

{

// Send Email Or Tweeting

}

}

}

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

}

}

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

}

}

}

InstallUtil -i MyServiceName.exe

InstallUtil -u MyServiceName.exe

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 :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.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 :Now to Install the service you will typeAnd then goto services.msc and start your installed service. To Uninstall the service you should runPlease Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.