标签:filter star register manage lin timertask pre cat 时间
安装:FluentScheduler
Install-Package FluentScheduler
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FluentScheduler; namespace ConsoleApp { class Program { static void Main(string[] args) { //两个参数分别:Job、调度时间 JobManager.AddJob(() => { //任务逻辑 Console.WriteLine("Timer task,current time:{0}", DateTime.Now); }, t => { //时间 //从程序启动开始执行,然后每秒钟执行一次 t.ToRunNow().AndEvery(1).Seconds(); ////带有任务名称的任务定时器 //t.WithName("TimerTask").ToRunOnceAt(DateTime.Now.AddSeconds(5)); }); Console.ReadKey(); } } }
1、任务逻辑
using System; using System.Diagnostics; using System.Threading; using FluentScheduler; namespace WebApp.Models { public class MyJob:Registry { public MyJob() { // 每2秒一次循环 Schedule<MyJob1>().ToRunNow().AndEvery(2).Seconds(); // 5秒后,只一次 Schedule<MyOtherJob>().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<MyFirstJob>().AndThen<MySecoundJob>().ToRunNow().AndEvery(5).Seconds(); } } public class MyJob1 : IJob { void IJob.Execute() { Trace.WriteLine("循环每2秒执行一次;现在时间是:" + DateTime.Now); } } public class MyOtherJob : IJob { void IJob.Execute() { Trace.WriteLine("5秒后只执行一次 ,现在时间是:" + DateTime.Now); } } public class MyFirstJob : IJob { void IJob.Execute() { Trace.WriteLine("执行第一个 Job ,现在时间是:" + DateTime.Now); } } public class MySecoundJob : IJob { void IJob.Execute() { Trace.WriteLine("执行第二个 Job ,现在时间是:" + DateTime.Now); } } }
2、初始化
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; using FluentScheduler; using WebApp.Models; namespace WebApp { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); JobManager.Initialize(new MyJob()); } } }
源码:
https://github.com/fluentscheduler/FluentScheduler
标签:filter star register manage lin timertask pre cat 时间
原文地址:http://www.cnblogs.com/xmai/p/7364829.html