I have included Quartz.net library in my C#.Net website to send emails automatically based on some conditions. I have started the job to with RepeatForever(). I am getting t
I wrote a set of methods to make it more easy.
You can't edit an existing trigger with your new interval, but you can replace your existing trigger with a new one.
The CreateTrigger
method creates a new Trigger based on a old one and transfers the TimesTriggered
count.
/// <summary>
/// Create a new trigger based on a existing trigger.
/// </summary>
/// <param name="oldTrigger">the existing trigger</param>
/// <param name="startNow">indicates if the trigger should start immediately after schedule</param>
/// <param name="interval">the interval for the new trigger</param>
/// <returns>Returns the new trigger</returns>
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;
}
How to use it
In this example, the Scheduler is already running.
// 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)));