我如何在quartz.net 2.6.1中停止工作.当WithRepeatCount完成后,即使我关闭应用程序,它仍然在运行背景.我的意思是我想在计数完成时运行scheduler.Shutdown().
private void Start_Click(object sender, RoutedEventArgs e)
{
Trigger trigger = new Trigger();
trigger.StartTrigger();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
Trigger trigger = new Trigger();
trigger.StopTrigger();
}
[DisallowConcurrentExecution]/**/
class Job : IJob
{
public void Execute(IJobExecutionContext context)
{
IsConnected.CheckConnection();
}
}
class Trigger
{
private IScheduler Start()
{
ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler sched = schedFact.GetScheduler();
sched.Start();
return sched;
}
private IScheduler Stop()
{
ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler sched = schedFact.GetScheduler();
sched.Shutdown();
return sched;
}
public void StartTrigger()
{
IScheduler sched = Start();
IJobDetail Job = JobBuilder.Create<Job>().WithIdentity("Job", null).Build();
ISimpleTrigger TriggerJob =
(ISimpleTrigger)TriggerBuilder
.Create()
.WithIdentity("Job")
.StartAt(DateTime.UtcNow)
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(1)
.WithRepeatCount(4)
)
.Build();
sched.ScheduleJob(Job, TriggerJob);
sched.Start();
}
public void StopTrigger()
{
IScheduler sched = Stop();
sched.Shutdown();
}
}
最佳答案 也许你必须看看石英
listeners并弄清楚如何计算:
IScheduler sched = Start();
IJobDetail jobDetail = GetJobDetail();
var listener = new CountJobListener();
sched.ListenerManager.AddJobListener(listener, KeyMatcher<JobKey>.KeyEquals(jobDetail.Key));
sched.Start();
实现自己的监听器:
internal class CountJobListener : IJobListener
{
public void JobToBeExecuted(IJobExecutionContext context)
{
}
public void JobExecutionVetoed(IJobExecutionContext context)
{
}
public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
{
if (GetCount() >= 4)
{
context.Scheduler.Shutdown();
}
}
private int GetCount()
{
throw new NotImplementedException();
}
public string Name { get { return "CountJobListener"; } }
}