Quartz.Net 洗掉Job
來博客園的第一篇文章先寫個簡單的,希望能幫助到大家,
步入正題:
Quartz.Net有三個重要的概念,分別是 Scheduler 、Job 、Trigger,
Scheduler包含Job和Trigger,
如果要洗掉一個正在運行的Job,需要在Schedule中將其移除,
呼叫的是IScheduler 中的DeleteJob 方法
上代碼:
public class JobController { private static JobController _jobController = new JobController(); private JobController() { } public static JobController Instance() { return _jobController; } public void Start() { LogProvider.SetCurrentLogProvider(new ConsoleLogProvider()); RunProgramRunExample().GetAwaiter().GetResult(); Console.WriteLine("Press any key to close the application"); Console.ReadKey(); } private static async Task RunProgramRunExample() { try { NameValueCollection props = new NameValueCollection { { "quartz.serializer.type", "binary" } }; StdSchedulerFactory factory = new StdSchedulerFactory(props); IScheduler scheduler = await factory.GetScheduler(); await scheduler.Start(); for (int i = 0; i < 10; i++) { IJobDetail job = JobBuilder.Create<AutoJob>() .WithIdentity("計算作業" + i.ToString(), "組1") .UsingJobData("jobSays", $"Hello {i}!") .UsingJobData("myFloatValue", i) .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity($"trigger{i}", "group1") .StartNow() .WithSimpleSchedule(x => x .WithIntervalInSeconds(10) .RepeatForever()) .Build(); // Tell quartz to schedule the job using our trigger await scheduler.ScheduleJob(job, trigger); } JobKey jk = new JobKey("計算作業1", "組1"); await scheduler.DeleteJob(jk);//移除一個job await Task.Delay(-1); await scheduler.Shutdown(); } catch (SchedulerException se) { Console.WriteLine(se); } } }
寫在最后:
第一次寫沒什么經驗,歡迎大家批評指教!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/114245.html
標籤:C#
