【问题标题】:How to stop a job when 'WithRepeatCount' done in C#在 C# 中完成“WithRepeatCount”时如何停止工作
【发布时间】:2017-10-16 13:26:14
【问题描述】:

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

【问题讨论】:

  • 后台运行是什么意思?您是否在进行线程转储并看到石英线程池仍然可用?
  • 我的意思是当运行线程的方法永无止境并且如果我关闭应用程序(btw wpf)它仍在运行,只有我可以在任务管理器中杀死它。
  • 目前还不清楚你想要实现什么。如果你只想调用一个方法 4 次,为什么不只使用一个简单的循环呢?在我看来,在运行后立即调用作业几次然后停止调度程序,这没有任何意义。顺便提一句。您在代码中调用 sched.Start(); 2 次。请说明您想要实现的目标。

标签: c# quartz-scheduler quartz.net


【解决方案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"; } }
}

【讨论】:

    【解决方案2】:

    如果 .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();
                }
    

    【讨论】:

      猜你喜欢
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多