C# FluentScheduler 实现任务计划

来源:互联网 发布:互联网金融大数据分析 编辑:程序博客网 时间:2024/04/29 19:01

1、简介

 FluentScheduler 是 .Net 开源的插件,插件地址:https://github.com/fluentscheduler/FluentScheduler


2、简单说明

using FluentScheduler;public class MyRegistry : Registry{    public MyRegistry()    {        // 立即执行每两秒一次的计划任务。(指定一个时间间隔运行,根据自己需求,可以是秒、分、时、天、月、年等。)        Schedule<MyJob>().ToRunNow().AndEvery(2).Seconds();        // 延迟一个指定时间间隔执行一次计划任务。(当然,这个间隔依然可以是秒、分、时、天、月、年等。)        Schedule<MyJob>().ToRunOnceIn(5).Seconds();        // 在一个指定时间执行计划任务(最常用。这里是在每天的 21:15 分执行)        Schedule(() => Console.WriteLine("It's 9:15 PM now.")).ToRunEvery(1).Days().At(21, 15);        // 立即执行一个在每月的星期一 3:00 的计划任务(可以看出来这个一个比较复杂点的时间,它意思是它也能做到!)        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();    }}