定时器之Quart.net(2)
第一步:Install-Package Quartz
namespace ProjectEdb { class Program { static void Main(string[] args) { //例子1 //QuartzJob.CreateJob("test", "zkb", "0 */1 * * * ?").GetAwaiter().GetResult(); //例子2 RunProgram().GetAwaiter().GetResult(); } } }
private static async Task RunProgram() { //int i = 0; DateTime myStartTime = Convert.ToDateTime("2016-12-08"); try { // Grab the Scheduler instance from the Factory//https://blog.csdn.net/u011966339/article/details/80911091 NameValueCollection props = new NameValueCollection { { "quartz.serializer.type", "binary" } }; StdSchedulerFactory factory = new StdSchedulerFactory(props); IScheduler scheduler = await factory.GetScheduler();//Scheduler:调度程序 // 启动任务调度器 await scheduler.Start(); // 定义一个 Job IJobDetail job = JobBuilder.Create<EdbQuartz>() .WithIdentity("job1", "group1") .Build(); ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger1") // 给任务一个名字 .StartAt(myStartTime) // 设置任务开始时间 .ForJob("job1", "group1") //给任务指定一个分组 .WithSimpleSchedule(x => x .WithIntervalInSeconds(20) //循环的时间 以秒为单位 .RepeatForever()) .Build(); // 等待执行任务 await scheduler.ScheduleJob(job, trigger); // some sleep to show what's happening //await Task.Delay(TimeSpan.FromMilliseconds(2000)); // and last shut down the scheduler when you are ready to close your program //await scheduler.Shutdown(); //i++; Console.ReadLine();//一定要加上 //Console.WriteLine(DateTime.Now.ToString() + "这是第" + i + "次执行任务"); } catch (SchedulerException se) { await Console.Error.WriteLineAsync(se.ToString()); } }
人各有命,上天注定,有人天生为王,有人落草为寇。脚下的路,如果不是你自己的选择,那么旅程的终点在哪,也没人知道。你会走到哪,会遇到谁,都不一定。