首先 Install-Package Quartz;
using System;using Quartz;using System.Collections.Specialized;using Quartz.Impl;using Common.Logging;namespace LisWms.Sch{ public class Scheduler { private static Scheduler instance; private static Object locker = new Object(); public static IScheduler scheduler; ////// 创建Scheduler /// ///public static Scheduler GetInstance() { lock (locker) { if (instance == null) { instance = new Scheduler(); } } return instance; } private Scheduler() { //工厂方式创建 IScheduler接口 ISchedulerFactory sf = new StdSchedulerFactory(); scheduler = sf.GetScheduler(); } public static void Start() { scheduler.Start(); } /// /// /// public static void Stop() { scheduler.Shutdown(true); instance = null; } ////// 暂停所有动做 /// public static void Pause() { scheduler.PauseAll(); } ////// 恢复所有动做 /// public static void Resume() { scheduler.ResumeAll(); } ////// 添加多个job任务 /// /// /// public static void AddJob(IJobDetail jobDetail, ITrigger trigger) { // 当Trigger认为应该触发的时候就会调用(实际上是Scheduler调用)job.execute方法了。 scheduler.ScheduleJob(jobDetail, trigger); } ////// 删除相关的job任务 /// /// ///public static bool DeleteJob(JobKey jobKey) { return scheduler.DeleteJob(jobKey); } }}
如上 Scheduler 类
设置 定时调度任务
//初始化 Scheduler.GetInstance(); #region AppUserJob //==========AppUserJob=========== IJobDetail AppUserJob = JobBuilder.Create() //创建一个作业 .WithIdentity("AppUserJob", "lisWmsSyn") .Build(); ITrigger AppUserJobTri = TriggerBuilder.Create() .WithIdentity("AppUserJobTri", "lisWmsSyn") .StartNow() .WithSimpleSchedule(x => x //触发时间,60秒一次。 .WithIntervalInSeconds(60) .WithMisfireHandlingInstructionFireNow() .RepeatForever()) //不间断重复执行 .Build(); //帮助类 HelpEvent help = new HelpEvent(); help.SetMsgEvent -= new HelpEvent.SetMsg(AddMsg); help.SetMsgEvent += new HelpEvent.SetMsg(AddMsg); Scheduler.scheduler.Context.Put("HelpEvent", help); Scheduler.AddJob(AppUserJob, AppUserJobTri); //把作业,触发器加入调度器。 #endregion
所有的任务类都要继承 IJob接口 不要对 Execute重写