C# Quartz.net修改任务时间
using System; using System.Threading.Tasks; using Quartz; using Quartz.Impl; using Quartz.Impl.Triggers; using Quartz.Logging; namespace QuartzSampleApp { public class Program { public static async void ModifyJobTime(string triggerName, string triggerGroupName, string jobName, string jobGroupName, TimeSpan cronTime) { //触发器的key TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroupName); //建立jobDetail JobDetailImpl jobDetail = new JobDetailImpl(); jobDetail.Name = jobName; jobDetail.Group = jobGroupName; jobDetail.JobType = typeof(HelloJob); //获取触发器 ITrigger trigger = await scheduler.GetTrigger(triggerKey); SimpleTriggerImpl cronTriggerImpl = (SimpleTriggerImpl)trigger; //移除任务 await scheduler.DeleteJob(jobDetail.Key); //修改时间 cronTriggerImpl.RepeatInterval = cronTime; //加入任务调度 await scheduler.ScheduleJob(jobDetail, cronTriggerImpl); } static IScheduler scheduler; private static async Task Main(string[] args) { //string formula = "20*(30-10)"; //var result = new System.Data.DataTable().Compute(formula, ""); //Console.WriteLine(result); LogProvider.SetCurrentLogProvider(new ConsoleLogProvider()); // Grab the Scheduler instance from the Factory StdSchedulerFactory factory = new StdSchedulerFactory(); scheduler = await factory.GetScheduler(); // and start it off await scheduler.Start(); // define the job and tie it to our HelloJob class IJobDetail job = JobBuilder.Create<HelloJob>() .WithIdentity("job1", "group1") .Build(); // Trigger the job to run now, and then repeat every 10 seconds ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartNow() .WithSimpleSchedule(x => x .WithIntervalInSeconds(3) .RepeatForever()) .Build(); // Tell quartz to schedule the job using our trigger await scheduler.ScheduleJob(job, trigger); // some sleep to show what's happening await Task.Delay(TimeSpan.FromSeconds(10)); Console.WriteLine("修改"); ModifyJobTime("trigger1", "group1", "job1", "group1", TimeSpan.FromSeconds(10)); // and last shut down the scheduler when you are ready to close your program //await scheduler.Shutdown(); await Task.Delay(TimeSpan.FromSeconds(60)); Console.WriteLine("Press any key to close the application"); Console.ReadKey(); } // simple log provider to get something to the console private class ConsoleLogProvider : ILogProvider { public Logger GetLogger(string name) { return (level, func, exception, parameters) => { if (level >= LogLevel.Info && func != null) { Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] [" + level + "] " + func(), parameters); } return true; }; } public IDisposable OpenNestedContext(string message) { throw new NotImplementedException(); } public IDisposable OpenMappedContext(string key, object value, bool destructure = false) { throw new NotImplementedException(); } } } public class HelloJob : IJob { public async Task Execute(IJobExecutionContext context) { await Console.Out.WriteLineAsync("Greetings from HelloJob!"); } } }