DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • 6 Steps to a Successful Career Change
  • Clustered Quartz Scheduler With Spring Boot and MongoDB
  • Python Asyncio: Understanding Concurrency With a Scraper
  • Every Acquisition Promises One Company. Most Deliver Two.

Trending

  • Your Cloud Infrastructure Is Elastic, and So Should Be Your Cost Governance
  • Passwordless Image Pulls in Kubernetes With ServiceAccount Tokens and a Credential Provider
  • Detecting and Measuring Bias in Training Data Before It Reaches Production
  • Function Calling: Responses API vs Chat Completions

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. Scheduling a Job Using The NCron Library

Scheduling a Job Using The NCron Library

By 
Ayobami Adewole
·
Apr. 18, 12 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
17.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Introduction

NCron is a .Net scheduling framework, it is a .Net version of Cron - the time based job scheduler found on unix like operating systems or Cron4j - scheduling library for Java. Ncron is light weight and easy to use, with little learning curve. It comes with some cool advantages, being that you can use it in C#, Vb.net or any other .Net programming language. It takes your mind off the details of scheduling and you can focus on how to implement the business logic of your application or the job to be scheduled. Details such as threading and timers have been taken care of.

Ncron Library

You can point your browser to http://code.google.com/p/ncron/downloads/detail?name=ncron-2.1.zip to download the ncron library. You need to add reference to the Ncron library in your project so as to be able to access the classes and functionalities of the Ncron scheduling framework.

Scheduling a Job

When creating a job to be scheduled using NCron, the job is wrapped up in a class which must extend the class NCron.CronJob and override a void method Execute
    public class MyJob : NCron.CronJob
    {
        public override void Execute()
        {
            System.IO.File.Copy(@"c:\\output.out", @"f:\\output.out");
        }
    }
        
The job to be scheduled will be placed in the Execute method. The next thing to do is to give NCron control over the job execution, by calling the static method Bootstrap.Init() at the entry point of your application, for example this can be put in the Main method. You should have a static setup  method, which I called JobSetup method that will be passed into the Bootstrap.Init() method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCron.Fluent.Crontab;
using NCron.Fluent.Generics;
using NCron.Service;

namespace NcronExample
{
    public class Program
    {
      private static void Main(string[] args)
      {
            Bootstrap.Init(args, JobSetup);
      }

      private static void JobSetup(SchedulingService schedulingService)
      {
            schedulingService.At("* * * * *").Run()<MyJob>;
      }
    }
}
        
The line of code inside the JobSetup method is to specify how the Job is going to be run, and the parameter in the schedulingService.At() method is known as crontab expression which I will discuss shortly. The SchedulingService class has a number of methods of interest.
       service.Daily().Run<MyJob>(); //runs the scheduled job once
                                               every day
       service.Hourly().Run<MyJob>(); //runs the scheduled job once
                                               every hour
       service.Weekly().Run<MyJob>(); //runs the scheduled job once
                                               every week
        

Crontab Expression

A crontab expression is a string comprising of 5 characters, which are seperated by space. This crontab expression when parsed produces occurrences of time based on a given schedule expressed in the crontab format. NCron parses crontab expression through the use of NCrontab(Crontab for .Net) an open source library for parsing crontab expressions.
A regular crontab expression is of the form

* * * * *

where the first * is for minute which can be from 0-59.
The second * is for hour which can also be from 0-23.
The third * is for day of the month from 1-31.
The fourth * is for month from 1-12.
The last * is for day of week from 0-6 where 0 represents Sunday.
The asterisk or wildcard character if left in the expression indicates all valid or legal values for that column.
If yIf you want the scheduled job to run every minute, the expresion will be in the form below.
* * * * *
The The expression below causes the scheduler to run the job at the fifth minute of every ninth hour everyday.
5 9 * * *
To run a job every tenth minute of every hour from Monday to Friday only, the expression will be in the form below.
10 * * * 1,2,3,4,5
You can read more on crontab expressions at http://code.google.com/p/ncrontab/wiki/CrontabExamples

Deploying the Scheduled Job

After the application has been built and compiled, you can deploy the scheduled job as a service by opening command prompt and change directory to where the executable of the application is  and then run the command.
ncronexample install
To install the scheduled job as a service, and that is it !!!
career job scheduling Library

Opinions expressed by DZone contributors are their own.

Related

  • 6 Steps to a Successful Career Change
  • Clustered Quartz Scheduler With Spring Boot and MongoDB
  • Python Asyncio: Understanding Concurrency With a Scraper
  • Every Acquisition Promises One Company. Most Deliver Two.

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook