定时任务FluentScheduler

1、Nuget 安装包

2、创建3个不同的任务

    public class MyJob : IJob
    {
        void IJob.Execute()
        {
            Trace.WriteLine("现在时间是:" + DateTime.Now);
        }
    }
    public class MyOtherJob : IJob
    {
        void IJob.Execute()
        {
            Trace.WriteLine("这是另一个 Job ,现在时间是:" + DateTime.Now);
        }
    }
    public class MyComplexJob : IJob
    {
        void IJob.Execute()
        {
            Trace.WriteLine("这是比较复杂的 Job ,现在时间是:" + DateTime.Now);
        }
    }

3、添加任务注册类,用于在Global中注册

    public class Demo : Registry
    {
        public Demo()
        {
            //利用反射获取所有的任务来执行
            var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IJob)))).ToArray();
            foreach (Type type in types)
            {
                //一个任务一个线程
                Task.Factory.StartNew(() =>
                { 
                    IJob ss = (IJob)Activator.CreateInstance(type);
                    Schedule(() =>
                    {
                        ss.Execute();
                    }).ToRunNow().AndEvery(2).Seconds();
                });
            }

            //// 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
            //// 在一个指定时间执行计划任务(最常用。这里是在每天的下午 1:10 分执行)
            //Schedule(() => Trace.WriteLine("It's 1:10 PM now.")).ToRunEvery(1).Days().At(13, 10);

            //Schedule(() =>
            //{

            //    // 做你想做的事儿。
            //    Trace.WriteLine("It's 1:10 PM now.");

            //}).ToRunEvery(1).Days().At(13, 10);

            //// Schedule a more complex action to run immediately and on an monthly interval
            //// 立即执行一个在每月的星期一 3:00 的计划任务(可以看出来这个一个比较复杂点的时间,它意思是它也能做到!)
            //Schedule<MyComplexJob>().ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);

            //// Schedule multiple jobs to be run in a single schedule
            //// 在同一个计划中执行两个(多个)任务
            //Schedule<MyJob>().AndThen<MyOtherJob>().ToRunNow().AndEvery(5).Minutes();

        }
    }

4、在 Global.asax 中注册

            JobManager.Initialize(new Demo());

其它:

也可以手动来进行启动、停止

        /// <summary>
        /// 启动定时任务
        /// </summary>
        public static void StartUp()
        {
            JobManager.Initialize(new Demo());
        }

        /// <summary>
        /// 停止定时任务
        /// </summary>
        public static void Stop()
        {
            JobManager.Stop();
        }

扩展:自定义时间及是否启用

    public class BaseJob
    {
        public int Second { get; set; }

        public bool Enabled { get; set; }

        public virtual void Execute() {
        }
    }
    public class MyJob : BaseJob
    {
        public MyJob() {
            Second = 5;
            Enabled = true;
        }
        public override void Execute()
        {
            Trace.WriteLine("MyJob 现在时间是:" + DateTime.Now);
        }
    }
    public class MyOtherJob : BaseJob
    {
        public MyOtherJob()
        {
            Second = 2;
            Enabled = true;
        }
        public override void Execute()
        {
            Trace.WriteLine("MyOtherJob 这是另一个 Job ,现在时间是:" + DateTime.Now);
        }
    }
        public Demo()
        {
            //利用反射获取所有的任务来执行
            //var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IJob)))).ToArray();
            var types = Assembly.GetExecutingAssembly().GetTypes()
                .Where(type => !String.IsNullOrEmpty(type.Namespace))
                .Where(type=>type.BaseType == typeof(BaseJob));
            foreach (Type type in types)
            {
                BaseJob ss = (BaseJob)Activator.CreateInstance(type);
                if (ss.Enabled)
                {
                    //一个任务一个线程
                    Task.Factory.StartNew(() =>
                    {
                        Schedule(() =>
                        {
                            ss.Execute();
                        }).ToRunNow().AndEvery(ss.Second).Seconds();
                    });
                }
            } 
        }
原文地址:https://www.cnblogs.com/ideacore/p/7652020.html