public class ExampleQuartzScheduler
{
private readonly ISchedulerFactory _schedulerFactory;
private IScheduler _scheduler = null;
public ExampleQuartzScheduler()
{
_schedulerFactory = new Quartz.Impl.StdSchedulerFactory();
}
public async void Start()
{
StopServiceSingleton.Stop = false;
if (_scheduler == null)
_scheduler = await _schedulerFactory.GetScheduler();
SetupExampleJob();
await _scheduler.Start();
}
private async void SetupExampleJob()
{
var jobKeyName = "Example-Job";
var triggerKeyName = $"ExampleJob-Trigger";
var triggerKey = new TriggerKey(triggerKeyName, jobKeyName);
// to avoid adding a job that is already added, un-schedule it first.
await _scheduler.UnscheduleJob(triggerKey);
IJobDetail job = JobBuilder.Create<MailSenderQuartzJobHandler>().WithIdentity(jobKeyName, jobKeyName).Build();
var trigger = TriggerBuilder.Create()
.WithIdentity(triggerKey)
.StartAt(DateTime.Now)
.WithSimpleSchedule(s => s.WithIntervalInSeconds(2).RepeatForever())
.Build();
await _scheduler.ScheduleJob(job, trigger);
}
public async void Stop()
{
if (_scheduler == null) return;
StopServiceSingleton.Stop = true;
// WHY DOES THIS NOT WAIT FOR ExampleJob TO FINISH?
await _scheduler.Shutdown(waitForJobsToComplete: true);
}
}
public partial class ExampleWindowsService : System.ServiceProcess.ServiceBase
{
private readonly ExampleQuartzScheduler _processor;
public ExampleWindowsService()
{
_processor = new ExampleQuartzScheduler();
}
protected override void OnContinue()
{
if (CanPauseAndContinue)
_processor.Start();
}
protected override void OnPause()
{
if (CanPauseAndContinue)
_processor.Stop();
}
protected override void OnShutdown()
{
if (CanShutdown)
_processor.Stop();
}
protected override void OnStart(string[] args)
{
_processor.Start();
}
protected override void OnStop()
{
if (CanStop)
_processor.Stop();
}
}
public class ExampleJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
var tasks = new List<Task>();
// example only
tasks.Add(Task.Factory.StartNew(() => { Thread.Sleep(20000); }, TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness));
await Task.WhenAll(tasks.ToArray());
}
}
--
You received this message because you are subscribed to the Google Groups "Quartz.NET" group.
To unsubscribe from this group and stop receiving emails from it, send an email to quartznet+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/quartznet/b4cdb97e-b12e-4a9a-999a-60893260e528o%40googlegroups.com.