Quarz的xml配置和程序集引用我觉得有点多,对于只需要执行一些简单任务的小白简直不能忍,无意间看到了这个FluentScheduler,于是动手试了试。
话不多说,直接上代码:
using System;using FluentScheduler;namespace HpTest.Fluent{ public class Klass { public Klass() { JobManager.Initialize(new MyRegistry()); } } public class MyRegistry : Registry { public MyRegistry() { // Schedule an ITask to run at an interval //Schedule().ToRunNow().AndEvery(2).Seconds(); // Schedule an ITask to run once, delayed by a specific time interval. //Schedule ().ToRunOnceIn(5).Seconds(); // Schedule a simple task to run at a specific time //Schedule(() => Console.WriteLine("Timed Task - Will run every day at 9:15pm: " + DateTime.Now)).ToRunEvery(1).Days().At(21, 15); // Schedule a more complex action to run immediately and on an monthly interval //Schedule(() => //{ // Console.WriteLine("Complex Action Task Starts: " + DateTime.Now); // Console.WriteLine("Complex Action Task Ends: " + DateTime.Now); //}).ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0); //Schedule multiple tasks to be run in a single schedule Schedule ().AndThen ().ToRunNow().AndEvery(5).Seconds(); } } public class MyTask : IJob { public static int MyTaskExeCount; public void Execute() { Console.WriteLine("这是MyTask正在第"+ MyTaskExeCount + "次执行"); MyTaskExeCount += 1; } } public class MyOtherTask : IJob { public void Execute() { Console.WriteLine("这是MyOtherTask正在执行"); } }}
然后直接在控制台Program中调用:
class Program { static void Main(string[] args) { new Klass(); Console.ReadKey(); } }
结果: