Skip to content

Tutorial

leosperry edited this page Jul 4, 2016 · 6 revisions

Working with Chroniton is easy.

Simply use the singularity to schedule a job and start the singularity.

###Get the singularity

    ISingularity singularity = Singularity.Instance;

or

    ISingularityFactory factory = new SingularityFactory();
    ISingularity singularity = factory.GetSingularity();

###Construct an IJob The library has a SimpleJob and SimpleParameterizedJob built for you which take Func<DateTime, Task> in their constructors. You can also create your own class which derives from IJob.

    var job = new SimpleJob(scheduledTime => Console.WriteLine(Task.Run(() => "Hello World")));

    var parameterizdeJob = new SimpleParameterizedJob<string>(
        (parameter, scheduledTime) => Task.Run(() => Console.WriteLine(parameter)));

###Construct an ISchedule You can use one of the built in schedules or create your own ISchedule. The ConstantSchedule will run a job continuously.

    var schedule = new ConstantSchedule();

Schedule the Job

The singularity does all the work of managing how jobs and schedules interact. It has several overloads for scheduling jobs.

    bool runNow = true;
    singularity.ScheduleJob(schedule, job, runNow); 

    var startTime = DateTime.UtcNow;
    singularity.ScheduleParameterizedJob(schedule, parameterizdeJob, "Hello World", startTime);

In the above code if runNow were set to false, the schedule would be called to get the first run. There are also overloads to make the first run happen at a specified time. ScheduleJob() returns an IScheduledJob which can be used to cancel the job if needed.

    singularity.StopScheduledJob(scheduledJob);

Start the singularity

    singularity.Start();
Clone this wiki locally