[Quick Tip] A job scheduler component for any .NET applications

September 26th, 2011 | Posted by Tom in .NET | Read: 941

Have you ever wonder how you could use a job scheduler in a .NET application ? Yes, it’s still possible to use workflow to get this behavior but, for some little projects, it might be a quite big technology.

So I’ve found Quartz.NET, which is described as “a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems”.

The documentation is very good, there are a lot of tutorials and the API is pretty simple and consist only in:

  1. Declaring a job (the code that need to be executed)
  2. Declaring a trigger (which specify when the task need to be executed)
  3. Indicate when the trigger need to be executed for the first time
  4. Scheduling the job

So, in a few lines of code:

// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();

// get a scheduler
IScheduler sched = schedFact.GetScheduler();
sched.Start();

// construct job info
JobDetail jobDetail = new JobDetail("myJob", null, typeof(HelloJob));
// fire every hour
Trigger trigger = TriggerUtils.MakeHourlyTrigger();
// start on the next even hour
trigger.StartTimeUtc = TriggerUtils.GetEvenHourDate(DateTime.UtcNow);
trigger.Name = "myTrigger";
sched.ScheduleJob(jobDetail, trigger);

I strongly recommend that you take a look at this component which is pretty robust and very useful !

 

Happy coding !

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Add Comment Register



Leave a Reply