Developed a basic, light-weight library for scheduling tasks in C# .Net

Source code can be found at GitHub

TaskSchedular

TaskScheduler is a simple and efficient C# .Net library that runs given tasks at the specified date and time.

  • Efficient : There is no polling. Only runs when a task is due. This is achieved though AutoResetEvent.
  • Simple : Merely 8 KB in size. Pretty easy to use but addresses limited number of use cases.

Background

.Net Framework comes with various Timer classes which give us the ability to run a task periodically.

Apart from Timers which run periodically, we don’t have any class which executes tasks at a given time. A usual work around is to use Timer of a second or so (based on your need), then keep checking if any task is due in timer’s event and execute the task when due. In order to be more resource friendly, TaskSchedular takes a different approach instead of continuous polling for task due date.

How TaskScheduler Works

It runs in it’s own thread and doesn’t consume any CPU resouces till a task is due. This is acheived through Wait Handle (AutoResetEvent). All scheduled tasks are executed in the same thread, which means:

  • tasks are not running in GUI thread so any code which is related to GUI should be invoked on the GUI thread. See this blog post for running code in GUI thread using Control.Invoke.
  • tasks are never executed in parallel as there is only one thread. This has following upshots:
    • saves us from thread synhronization issues within tasks.
    • this library might not the the right one for you if you need more parallelism. In such a case, check out alternatives like Quartz.NET and FluentScheduler.

Usage

Starting TaskScheduler
var schedular = new TaskSchedular.TaskSchedular();
schedular.Start();
Adding task
schedular.AddTask(new TaskSchedular.Task()
    {
        StartTime = DateTime.Now.AddSeconds(30),
        TaskAction = () =>
        {
            // do some work here
            System.Threading.Thread.Sleep(300);
        },
        Recurrance = TimeSpan.FromSeconds(30)
    });

Note: TaskSchedular is has a tolerance of 1 second by default, that is, if a task is due within a second, it will execute it right away.

One thought on “Developed a basic, light-weight library for scheduling tasks in C# .Net

Leave a Reply

Your email address will not be published. Required fields are marked *