1. 入门案例
using Quartz;
using Quartz.Impl;
public class PrintTime : IJob { public Task Execute(IJobExecutionContext context) { return Task.Factory.StartNew(new Action(() => Console.WriteLine($"当前时间: {DateTime.Now.ToString()}"))); } }
static void Main(string[] args) { startShowTime(); Console.ReadKey(); } static async void startShowTime() { ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); IScheduler scheduler = await schedulerFactory.GetScheduler(); IJobDetail job = JobBuilder.Create<PrintTime>().Build(); try { ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create().WithSimpleSchedule(x => x.WithIntervalInSeconds(3).WithRepeatCount(50)).Build(); await scheduler.ScheduleJob(job, trigger); await scheduler.Start(); } catch (Exception EX) { Console.WriteLine(EX.Message); await scheduler.Shutdown(); } }
2. 运行结果