.net下定时任务 Quartz.Net 使用 cron表达式
Nuget引用 Quartz
using Quartz; using Quartz.Impl; using System; using System.Threading; using System.Threading.Tasks; namespace ConsoleApp3 { class Program { static void Main(string[] args) { ISchedulerFactory sf = new StdSchedulerFactory(); IScheduler scheduler = sf.GetScheduler().ConfigureAwait(false).GetAwaiter().GetResult(); IJobDetail job = JobBuilder.Create<TimingJob>().WithIdentity("job1", "mygroup").Build(); var cronExpression = "/2 * * ? * *"; ITrigger trigger = TriggerBuilder.Create().StartAt(DateTime.Now.AddSeconds(5)).WithCronSchedule(cronExpression).Build(); scheduler.ScheduleJob(job, trigger); scheduler.Start(); Thread.Sleep(10000000); } } /// <summary> /// 定时任务类 /// </summary> public class TimingJob : IJob { Task IJob.Execute(IJobExecutionContext context) { return Task.Run(() => { Console.WriteLine("TimingJob run"); }); } } }