如何在WithRepeatCount'以及#39; WithRepeatCount'在C#中完成

时间:2017-10-16 13:26:14

标签: c# quartz-scheduler quartz.net

我如何在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();
    }
}

2 个答案:

答案 0 :(得分:1)

也许你必须看一下石英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"; } }
}

答案 1 :(得分:0)

如果 .WithIntervalInSeconds(1).WithRepeatCount(4)表示它将运行5秒,那么它仍会在后端永远运行。如果我添加 Thread.Sleep(4000) );和sched.Shutdown(); 在sched.Start(); 之后它将运行5秒然后它将被关闭。代码应该是这样的;

  public void StartTrigger()
        {
            try
            {
                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)
                                   .RepeatForever()
                                   //.WithRepeatCount(4)
                                   .WithMisfireHandlingInstructionNextWithExistingCount())
                                   .Build();
                sched.ScheduleJob(Job, TriggerJob);
                sched.Start();
                Thread.Sleep(4T000);
                sched.Shutdown();
            }