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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • 6 Steps to a Successful Career Change
  • Clustered Quartz Scheduler With Spring Boot and MongoDB
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Understanding ldd: The Linux Dynamic Dependency Explorer

Trending

  • AI, ML, and Data Science: Shaping the Future of Automation
  • Scaling Mobile App Performance: How We Cut Screen Load Time From 8s to 2s
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • Optimize Deployment Pipelines for Speed, Security and Seamless Automation
  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 user avatar
Ayobami Adewole
·
Apr. 18, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
17.1K 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.

Related

  • 6 Steps to a Successful Career Change
  • Clustered Quartz Scheduler With Spring Boot and MongoDB
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Understanding ldd: The Linux Dynamic Dependency Explorer

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!