Git Product home page Git Product logo

taskscheduler's Introduction

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.

Build status

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)
    });

#Notes

  • TaskSchedular has a tolerance of 1 second by default, that is, if a task is due within a second, it will execute it right away.
  • Tasks can be added to scheduler before starting it. Once the scheduler is started, any overdue task will be executed immediately.

taskscheduler's People

Contributors

umaranis avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.