下面描述了一个Quartz.net最常用的应用场景,按特定秒数触发执行特定任务。
1.任务要继承自:IJob,在Execute方法中填入自己需要执行的任务。
public virtual void Execute(JobExecutionContext context) { try { string jobName = context.JobDetail.FullName; _log.Info("Executing job: " + jobName + " executing at " + DateTime.Now.ToString("r")); } catch (Exception e) { _log.Info("--- Error in job!"); } }
核心代码:
ISchedulerFactory factory = new StdSchedulerFactory(); IScheduler scheduler = factory.GetScheduler(); scheduler.Start(); JobDetail job = new JobDetail("MyJob", "group1", typeof(SimpleQuartzJob)); Trigger trigger = TriggerUtils.MakeMinutelyTrigger(1); trigger.StartTime = DateTime.Now.AddSeconds(5); trigger.Name = "ShiftNotify"; scheduler.ScheduleJob(job, trigger);
附上全部代码:下载