我写了一套方法让它变得更容易.您无法使用新间隔编辑现有触发器,但可以使用新触发器替换现有触发器.该CreateTrigger
方法基于旧的触发器创建新的触发器并传输TimesTriggered
计数.
////// Create a new trigger based on a existing trigger. /// /// the existing trigger /// indicates if the trigger should start immediately after schedule /// the interval for the new trigger ///Returns the new trigger public ITrigger CreateTrigger(ITrigger oldTrigger, bool startNow = false, TimeSpan? interval = null) { var builder = oldTrigger.GetTriggerBuilder(); if (startNow) builder = builder.StartNow(); if (interval.HasValue) builder = builder.WithSimpleSchedule(s => s .WithInterval(interval.Value) .RepeatForever()); var newTrigger = builder.Build(); var simpleTrigger = newTrigger as ISimpleTrigger; if(simpleTrigger != null) { var trigger = oldTrigger as ISimpleTrigger; if (trigger != null) simpleTrigger.TimesTriggered = trigger.TimesTriggered; } return newTrigger; }
如何使用它
在此示例中,Scheduler已在运行.
// get the trigger var trigger = Scheduler.GetTrigger(new TriggerKey("trigger1", "group1")); // get your settings and set the interval var reminderSettings = GetReminderSettings(); var intervalBtwnReminders = 0; if (reminderSettings?.RemindersGap != null) { intervalBtwnReminders = (int)(reminderSettings.RemindersGap); } // reschedule the job with a new trigger and start it immediately. // if you don't want that it starts now, pass 'false' for the 'startNow' parameter Scheduler.RescheduleJob(trigger.Key, CreateTrigger(trigger, true, TimeSpan.FromMinutes(intervalBtwnReminders)));