FluentScheduler是一个简单的任务调度框架,使用起来非常方便。作者的源码和例子的地址:

https://github.com/fluentscheduler/FluentScheduler

1.首先引用FluentScheduler.dll,dll数据源可通过NuGet程序包获取。打开管理解决方案的NuGet程序包,输入FluentScheduler即可。

步骤:状态栏选择 工具 - NuGet程序包管理器 – 管理解决方案的NuGet程序包,然后输入FluentScheduler即可。

FluentScheduler实现定时任务

 2.新建Registry继承类

using System;
using FluentScheduler;
using System.Threading;

namespace DemoProject2020
{
    public class TimingRoutine:Registry
    {
        public  TimingRoutine()
        {
            Welcome();
        }
        private void Welcome()
        {
            // 每2秒一次循环
            Schedule<TaskJob>().ToRunNow().AndEvery(2).Seconds();

            // 5秒后,只一次
            Schedule<TaskJob>().ToRunOnceIn(5).Seconds();

            //每天晚上21:15分执行
            Schedule(() => Console.WriteLine("Timed Task - Will run every day at 9:15pm: " + DateTime.Now)).ToRunEvery(1).Days().At(21, 15);

            // 每个月的执行
            Schedule(() =>
            {
                Console.WriteLine("Complex Action Task Starts: " + DateTime.Now);
                Thread.Sleep(1000);
                Console.WriteLine("Complex Action Task Ends: " + DateTime.Now);
            }).ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);

            //先执行第一个Job、再执行第二个Job;完成后等5秒继续循环
            Schedule<TaskJob>().AndThen<TaskJob>().ToRunNow().AndEvery(5).Seconds();
            //直接运行
            Schedule(() => Console.Write("3, "))
                .WithName("[welcome]")
                .AndThen(() => Console.Write("2, "))
                .AndThen(() => Console.Write("1, "))
                .AndThen(() => Console.WriteLine("Live!"));
        }
    }
    
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-21
  • 2022-12-23
  • 2022-12-23
  • 2022-02-21
  • 2022-12-23
  • 2021-07-15
相关资源
相似解决方案