标签:settings task tin 理想 evel ati div async shu
1 public class UpdateJob : IJob 2 { 3 4 public async Task Execute(IJobExecutionContext context) 5 { 6 try 7 { 8 await Task.Run(() => UpdateJobFun()); 9 } 10 catch (Exception ex) 11 { 12 await Task.Run(() => LogHelper.WriteLogs("")); 13 throw ex; 14 } 15 } 16 public void UpdateJobFun() 17 { 18 //日志记录 19 LogHelper.WriteLogs("当前时间为:"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); 20 } 21 }
1 public class JobScheduler 2 { 3 //调度器工厂 4 private static readonly ISchedulerFactory factory = null; 5 //调度器 6 private static readonly IScheduler scheduler = null; 7 //定时规则 8 public static string QuartzCron = 9 ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["QuartzCron"].Value; // 10 static JobScheduler() 11 { 12 //创建一个工厂 13 factory = new StdSchedulerFactory(); 14 scheduler = factory.GetScheduler().Result; 15 scheduler.Start(); 16 } 17 public static void Start() 18 { 19 20 scheduler.GetJobGroupNames(); 21 IJobDetail job; 22 ITrigger trigger; 23 //创建任务 24 //Quartz.JobBuilder.Create<UpdateJob>().Build(); 25 job = JobBuilder.Create<UpdateJob>().Build(); 26 //创建触发器 27 trigger = TriggerBuilder.Create() 28 .WithIdentity("TimeTrigger", "TimeGroup") 29 //.WithSimpleSchedule(t => t.WithIntervalInMinutes(1).RepeatForever()) 30 .WithCronSchedule(QuartzCron) 31 .Build(); 32 //添加任务及触发器至调度器中 33 scheduler.ScheduleJob(job, trigger); 34 //启动 35 scheduler.Start(); 36 37 } 38 public static void OnStop() 39 { 40 if (!scheduler.IsShutdown) 41 { 42 scheduler.Shutdown(); 43 } 44 } 45 }
1 public partial class QuartzService : ServiceBase 2 { 3 4 private IScheduler scheduler; 5 public QuartzService() 6 { 7 InitializeComponent(); 8 9 } 10 protected override void OnStart(string[] args) 11 { 12 JobScheduler.Start(); 13 } 14 protected override void OnStop() 15 { 16 JobScheduler.OnStop(); 17 } 18 }
标签:settings task tin 理想 evel ati div async shu
原文地址:https://www.cnblogs.com/ywkcode/p/12129084.html