The job configuration is handled in a Registry class. A job is either an Action or a class that inherits IJob:

using FluentScheduler;

public class MyRegistry : Registry
{
public MyRegistry()
{
// Schedule an IJob to run at an interval
Schedule<MyJob>().ToRunNow().AndEvery(2).Seconds(); // Schedule an IJob to run once, delayed by a specific time interval
Schedule<MyJob>().ToRunOnceIn(5).Seconds(); // Schedule a simple job to run at a specific time
Schedule(() => Console.WriteLine("It's 9:15 PM now.")).ToRunEvery(1).Days().At(21, 15); // Schedule a more complex action to run immediately and on an monthly interval
Schedule<MyComplexJob>().ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0); // Schedule a job using a factory method and pass parameters to the constructor.
Schedule(() => new MyComplexJob("Foo", DateTime.Now)).ToRunNow().AndEvery(2).Seconds(); // Schedule multiple jobs to be run in a single schedule
Schedule<MyJob>().AndThen<MyOtherJob>().ToRunNow().AndEvery(5).Minutes();
}
}

You can also use the Registry class directly (instead of inheriting it):

var registry = new Registry();
registry.Schedule<MyJob>().ToRunNow().AndEvery(2).Seconds();
registry.Schedule<MyJob>().ToRunOnceIn(5).Seconds();
registry.Schedule(() => Console.WriteLine("It's 9:15 PM now.")).ToRunEvery(1).Days().At(21, 15);
registry.Schedule<MyComplexJob>().ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);
registry.Schedule<MyJob>().AndThen<MyOtherJob>().ToRunNow().AndEvery(5).Minutes();

With the registry ready you then need to initialize the JobManager. This is usually done as soon as your application is loaded (in the Application_Start method of a web application for example):

protected void Application_Start()
{
JobManager.Initialize(new MyRegistry());
}

It's also possible to schedule jobs after initialization:

JobManager.AddJob(() => Console.WriteLine("Late job!"), (s) => s.ToRunEvery(5).Seconds());

Using it with ASP.NET

When using it with ASP.NET consider implementing IRegisteredObject and registering it on HostingEnvironment (here's why), like:

public class SampleJob : IJob, IRegisteredObject
{
private readonly object _lock = new object(); private bool _shuttingDown; public SampleJob()
{
// Register this job with the hosting environment.
// Allows for a more graceful stop of the job, in the case of IIS shutting down.
HostingEnvironment.RegisterObject(this);
} public void Execute()
{
try
{
lock (_lock)
{
if (_shuttingDown)
return; // Do work, son!
}
}
finally
{
// Always unregister the job when done.
HostingEnvironment.UnregisterObject(this);
}
} public void Stop(bool immediate)
{
// Locking here will wait for the lock in Execute to be released until this code can continue.
lock (_lock)
{
_shuttingDown = true;
} HostingEnvironment.UnregisterObject(this);
}
}

Using it with .NET Core

FluentScheduler supports .NET Core, just add the dependency to project.json and run dotnet restore:

  "dependencies": {
"FluentScheduler": "<desired version>"
}

Stopping the Scheduler

To stop the scheduler:

JobManager.Stop();

To both stop and wait for the running jobs to finish:

JobManager.StopAndBlock();

Unexpected exceptions

To observe unhandled exceptions from your scheduled jobs listen for the JobException event on JobManager:

JobManager.JobException += info => Log.Fatal("An error just happened with a scheduled job: " + info.Exception);

Concurrent jobs

By default, the library allows a schedule to run in parallel with a previously triggered execution of the same schedule.

If you don't want such behaviour you can set a specific schedule as non-reentrant:

public class MyRegistry : Registry
{
Schedule<MyJob>().NonReentrant().ToRunEvery(2).Seconds();
}

Or you can set it to all schedules of the registry at once:

public class MyRegistry : Registry
{
NonReentrantAsDefault();
Schedule<MyJob>().ToRunEvery(2).Seconds();
}

Daylight Saving Time

Unfortunately, not unlike many schedulers, there is no daylight saving time support yet.

If you are worried about your jobs not running or running twice due to that, the suggestion is to either avoid troublesome time ranges or to force the use of UTC:

JobManager.UseUtcTime();

Milliseconds Accuracy

The aim of the library is ease of use and flexibility, and not millisecond precision. While the library has a millisecond unit, you should avoid relying on really small intervals (less than 100ms).

Weekly jobs

Let's suppose it's 10:00 of a Monday morning and you want to start a job that runs every Monday at 14:00. Should the first run of your job be today or only on the next week Monday?

If you want the former (not waiting for a whole week):

// Every "zero" weeks
Schedule<MyJob>().ToRunEvery(0).Weeks().On(DayOfWeek.Monday).At(14, 0);

Or if you want the latter (making sure that at least one week has passed):

// Every "one" weeks
Schedule<MyJob>().ToRunEvery(1).Weeks().On(DayOfWeek.Monday).At(14, 0);

Dependency Injection

Currently, the library supports dependency injection of jobs (via IJobFactory). However, you shouldn't use it, it's on its way to be deprecated (here's why).

Contributing

Feel free to open an issue or to submit a pull request.

To make sure your pull request doesn't get rejected, discuss it in an issue beforehand. Also, remember to Run All Tests (Ctrl + R, A)Run Code Analysis on Solution (Alt + F11) and to be consistent with the existing code.

You can also help others in need for support, check the "help wanted" label.

// 立即执行计划任务,并根据指定时间间隔执行一次计划任务

//(指定一个时间间隔运行,根据自己需求,可以是秒、分、时、天、月、年等。)

Schedule().ToRunNow().AndEvery(10).Seconds();//每十秒运行一次

// 延迟一个指定时间间隔执行一次计划任务。

//(当然,这个间隔依然可以是秒、分、时、天、月、年等。)

Schedule().ToRunOnceIn(10).Seconds();

//在一个指定时间执行计划任务 每个小时的第10分钟执行

Schedule().ToRunEvery(1).Hours().At(46);

// 在一个指定时间执行计划任务(最常用。这里是在每天的下午 1:10 分执行) Schedule().ToRunEvery(1).Days().At(13,10);

//每n年的第几天

Schedule().ToRunEvery(1).Years().On(5).At(12, 00);

//每n年的最后一天

Schedule().ToRunEvery(1).Years().OnTheLastDay();

//每n月的第几天

Schedule().ToRunEvery(1).Months().On(1).At(12,0);

//每n月的第一个星期的星期5 的15:0执行Schedule().ToRunEvery(1).Months().OnTheFirst(DayOfWeek.Friday).At(15, 0);

//每n月的第一个星期的星期5 的15:0执行 CleanJob和TestJob

Schedule().AndThen().ToRunEvery(1).Months().OnTheFirst(DayOfWeek.Friday).At(15, 0);

//在每天3点清理数据

Schedule().ToRunNow().AndEvery(10).Days().At(3,
00);

最后还要在项目的Global.asax那注册定时任务

双击进出,在里面注册定时任务

FluentScheduler的更多相关文章

  1. C#定时任务组件之FluentScheduler

    FluentScheduler是.NET开源处理定时任务组件 1.任务的创建注册 public static void TaskActionByMinutes(Action action, int c ...

  2. ASP.NET MVC 使用 FluentScheduler 定时器计划任务

    MacBook Pro 只有四个 USB Type-C 接口是否错了? 一项新技术的诞生总会对已存在的事物造成冲击或影响,如果大家都害怕冲击与影响,那这个世界永远像现在不变就行了,大家都好好的,待在自 ...

  3. .NET定时任务执行管理器开源组件–FluentScheduler

    在日常项目里通常会遇到定时执行任务的需求,也就是定时器..NET Framework里关于定时器的类有3个,分别是System.Windows.Forms.Timer.System.Timers.Ti ...

  4. 不用写Windows服务实现定时器功能(FluentScheduler )

    MacBook Pro 只有四个 USB Type-C 接口是否错了? 一项新技术的诞生总会对已存在的事物造成冲击或影响,如果大家都害怕冲击与影响,那这个世界永远像现在不变就行了,大家都好好的,待在自 ...

  5. [转]推荐一个简单、轻量、功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler

    在C#WINFORM或者是ASP.NET的WEB应用程序中,根据各种定时任务的需求,比如:每天的数据统计,每小时刷新系统缓存等等,这个时候我们得应用到定时器这个东东. .NET Framework有自 ...

  6. 推荐一个简单、轻量、功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler定时器

    在C#WINFORM或者是ASP.NET的WEB应用程序中,根据各种定时任务的需求,比如:每天的数据统计,每小时刷新系统缓存等等,这个时候我们得应用到定时器这个东东. .NET Framework有自 ...

  7. 在吉日嘎拉DotNet.WebForm中使用FluentScheduler调度任务

    有些用户一直说系统发送的邮件一直收不到,投诉系统不正常,这时候怎么洗刷冤屈呢?将发送的每一封Email都保存到数据库中,并记录发送的日志,让用户无话可说. 自己创建3个表: MessageFailed ...

  8. MVC 使用 FluentScheduler 定时器计划任务

    MVC 使用 FluentScheduler 定时器计划任务 MacBook Pro 只有四个 USB Type-C 接口是否错了? 一项新技术的诞生总会对已存在的事物造成冲击或影响,如果大家都害怕冲 ...

  9. 【转】ASP.NET MVC 使用 FluentScheduler 定时器计划任务

    MacBook Pro 只有四个 USB Type-C 接口是否错了? 一项新技术的诞生总会对已存在的事物造成冲击或影响,如果大家都害怕冲击与影响,那这个世界永远像现在不变就行了,大家都好好的,待在自 ...

  10. 定时任务FluentScheduler 学习笔记 .net

    第一步添加引用 GitHub源码地址 与详细用法 https://github.com/fluentscheduler/FluentScheduler 下面开始简单的实现 /// <summar ...

随机推荐

  1. yield python

    原文:http://pyzh.readthedocs.io/en/latest/the-python-yield-keyword-explained.html 3. (译)Python关键字yield ...

  2. XenServer修改DNS

    XenServer没法直接修改DNS,感觉好奇怪啊 修改方法: 1.进入命令行:  2.执行命令:      # xe pif-list 列出网卡的UUID.  3.执行命令:      # xe p ...

  3. 编程实战——电影管理器之XML存储电影信息数据

    但凡管理器之类的软件,存储数据是必不可少的.存储数据的话,有几种选择.一是用数据库,把数据存储到数据库里:一是用文本文件,把数据存储到文本文件里:一种是利用XML文件,把数据对象转换为XML后,存储到 ...

  4. easyui tree loader用法

    easyui的tree每次都展开,在获取子节点,自定义参数解决方案,兄跌是不是找很久了! 直接上代码 //重写tree的loader $.extend($.fn.tree.defaults, { lo ...

  5. 使用Snap.svg类库实现的抖动式的幻灯播放效果

    在线演示 本地下载 这个幻灯中,使用了SVG来生成具有动画弧度的幻灯背景效果,如果你在项目中能够支持现代浏览器的话,尝试一下这个效果吧,很赞! 想了解基础使用,观看这个轻视频吧:Snap.svg处理和 ...

  6. python 微信企业号

    python 微信企业号 准备,如果没有微信企业号,可以先申请体验号记下CorpID和Secret(获取Token用) 发送消息首先可以在微信的开发者中心,查看接口文档 下面就是python代码:1. ...

  7. .geodatabase与gdb的相互转换

    .geodatabase长得是gdb的全称,确实它们有一定的关系,但也有区别. 简单认识一下 有人也问过我,gdb外表像个文件夹,是怎么实现的.gdb数据库是ESRI特有的数据库,它是一些数据集定义. ...

  8. android中使用Nine-Patch图片

    android中可以把图片进行处理,如果图片被拉伸的话,允许让图片部分区域不拉伸,部分区域拉伸.这个功能非常好,比如聊天的气泡,如果整个气泡被拉伸的话,会非常的丑. 老版的sdk中提供的有draw9p ...

  9. 关于Chrome浏览器(Chrome Stable、 Chrome Canary 、Chromium)

    作为开发者,web浏览器一般最常用的可能是Chrome浏览器.但其实Chrome浏览器还有别的一些版本.如:Chrome Stable. Chrome Canary .Chromium.大部分人一般用 ...

  10. Java多线程-BlockingQueue-ArrayBlockingQueue-LinkedBlockingQueue

    前言: BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利.本文详细介绍了Blocking ...