Quartz – scheduling tasks in C#

Quartz is a scheduling library. You can run your task periodically.
This is how scheduler and it’s triggers can be configured.

        private static void ConfigureScheduler()
        {
            var scheduler = new StdSchedulerFactory().GetScheduler();
            var job = JobBuilder.Create<PrintDateTimeJob>()
                .WithIdentity("MyJob")
                .Build();

            var trigger = TriggerBuilder.Create()
                .StartAt(new DateTimeOffset(2000, 1, 1, 10, 0, 0, TimeSpan.FromSeconds(0)))
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
                .WithIdentity("MyTrigger")
                .Build();

            scheduler.ScheduleJob(job, trigger);

            scheduler.Start();
        }

Your task class must implement IJob interface.

    class PrintDateTimeJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Current date and time: " + DateTime.Now.ToLongTimeString());
        }
    }

Code above will schedule simple job (printing current time) every 5 seconds. This is a simple example only. For detailed information go to: Quartz.NET.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s