TaskScheduler是一个.net版的任务调度器。概念少,简单易用。
支持SimpleTrigger触发器,指定固定时间间隔和执行次数;
支持CronTrigger触发器,用强大的Cron表达式满足日历形式的复杂触发规则;
支持动态添加和删除任务,可根据具体需求实现复杂逻辑;
static void Main(string[] args) { Scheduler scheduler = new Scheduler(); SimpleTrigger simpleTrigger = new SimpleTrigger(2, 3); CronTrigger cronTrigger = new CronTrigger("0/5 * * ? * MON-FRI"); scheduler.AddJob("job1", cronTrigger, (sch, trigger) => { Console.WriteLine("cronTrigger触发,工作日每隔5秒"); }); scheduler.AddJob("job2", simpleTrigger, (sch, trigger) => { Console.WriteLine("simpleTrigger触发,每隔两秒,本来要执行三次的"); Console.WriteLine("终结job2"); sch.RemoveJob("job2"); Console.WriteLine("动态加入jobnew"); scheduler.AddJob("jobnew", new SimpleTrigger(2, 2), (s, t) => { Console.WriteLine("jobnew代替job2,执行后两次"); }); }); scheduler.Start(); Console.ReadLine(); }