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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Never Use Credentials in a CI/CD Pipeline Again
  • Web Development Checklist
  • Auto-Scaling Kinesis Data Streams Applications on Kubernetes
  • Micro Frontends on Monorepo With Remote State Management

Trending

  • Never Use Credentials in a CI/CD Pipeline Again
  • Web Development Checklist
  • Auto-Scaling Kinesis Data Streams Applications on Kubernetes
  • Micro Frontends on Monorepo With Remote State Management
  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. Scheduling a Job Using The NCron Library

Scheduling a Job Using The NCron Library

Ayobami Adewole user avatar by
Ayobami Adewole
·
Apr. 18, 12 · Interview
Like (0)
Save
Tweet
Share
16.09K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Trending

  • Never Use Credentials in a CI/CD Pipeline Again
  • Web Development Checklist
  • Auto-Scaling Kinesis Data Streams Applications on Kubernetes
  • Micro Frontends on Monorepo With Remote State Management

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: