title: Stop Quartz job if it is successfully finished
body: <p>How to stop job if certain condition is satisfied?</p>
  
  <p>Here is one example that runs job every 5 second and generate random number between 1 and 100. Now I want when generated number is let's say 50 to stop the job.</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          JobScheduler jobScheduler = new JobScheduler();
          jobScheduler.Start();
          Console.ReadLine();
      }
  }
  
  public class JobScheduler
  {
      public async void Start()
      {
          ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
          IScheduler scheduler = await schedulerFactory.GetScheduler();
          await scheduler.Start();
  
  
          string id_job1 = Guid.NewGuid().ToString();
  
          // define the job and tie it to our HelloJob class
          IJobDetail job1 = JobBuilder.Create&lt;HelloJob&gt;()
              .WithIdentity(Guid.NewGuid().ToString(), "group1")
              .Build();
  
          // Trigger the job to run now, and then every 5 seconds
          ITrigger trigger1 = TriggerBuilder.Create()
              .WithIdentity(id_job1, "group1")
              .StartNow()
              .WithSimpleSchedule(x =&gt; x
                  .WithIntervalInSeconds(5)
                  .RepeatForever())
          .Build();                       
  
  
          var listener = new CountJobListener();
          scheduler.ListenerManager.AddJobListener(listener, KeyMatcher&lt;JobKey&gt;.KeyEquals(job1.Key));
  
          await scheduler.ScheduleJob(job1, trigger1);          
  
      }
  }
  
  public class HelloJob : IJob
  {
      SomethingToDo obj = new SomethingToDo();
      public async Task Execute(IJobExecutionContext context)
      {           
          await Console.Out.WriteLineAsync("HelloJob is executing.");
          int num = obj.CreateRandomNumbers();
          Console.WriteLine(num);
      }
  }
  
  public class CountJobListener : IJobListener
  {       
  
      public async Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
      {
  
      }
  
      public async Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
      {
  
      }
  
      public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken))
      {
  
      }
  
      public string Name { get { return "CountJobListener"; } }
  
  }
  
  public class SomethingToDo
  {
      public int CreateRandomNumbers()
      {
          return new Random().Next(1, 101);
      }
  }
  </code></pre>
  
  <p>How to achieve this?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>class Program
  {
              static void Main(string[] args)
              {
                  JobScheduler jobScheduler = new JobScheduler();
                  jobScheduler.Start();
                  Console.ReadLine();
              }
          }
  
          public class JobScheduler
          {
  
              public async void Start()
              {
                  ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
                  IScheduler scheduler = await schedulerFactory.GetScheduler();
                  await scheduler.Start();
  
  
                  string id_job1 = Guid.NewGuid().ToString();
  
                  // define the job and tie it to our HelloJob class
                  IJobDetail job1 = JobBuilder.Create&lt;HelloJob&gt;()
                      .WithIdentity(Guid.NewGuid().ToString(), "group1")
                      .Build();
  
                  // Trigger the job to run now, and then every 5 seconds
                  ITrigger trigger1 = TriggerBuilder.Create()
                      .WithIdentity(id_job1, "group1")
                      .StartNow()
                      .WithSimpleSchedule(x =&gt; x
                          .WithIntervalInSeconds(5)
                          .RepeatForever())
                  .Build();                       
  
  
                  var listener = new CountJobListener();
                  scheduler.ListenerManager.AddJobListener(listener, KeyMatcher&lt;JobKey&gt;.KeyEquals(job1.Key));
  
                  await scheduler.ScheduleJob(job1, trigger1);         
  
              }
          }
  
          public class HelloJob : IJob
          {
              SomethingToDo obj = new SomethingToDo();
              public async Task Execute(IJobExecutionContext context)
              {           
                  await Console.Out.WriteLineAsync("Job is executing.");
                  int num = obj.CreateRandomNumbers();
                  context.JobDetail.JobDataMap["GeneratedNumber"] = num;  // Store some value in JobMap to be available for further processing if necessary
                  Console.WriteLine(num);
  
                  if (num == 5)
                      await context.Scheduler.PauseJob(context.JobDetail.Key);
  
              }
          }
  
          public class CountJobListener : IJobListener
          {       
  
              public async Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
              {
  
              }
  
              public async Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
              {
  
              }
  
              public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken))
              {
                  int retVal = Convert.ToInt32(context.JobDetail.JobDataMap["GeneratedNumber"].ToString());
  
                  if (retVal == 50)
                  {
                      await Console.Out.WriteLineAsync("Succesfully completed!");
                  }
  
              }
  
              public string Name { get { return "CountJobListener"; } }
  
          }
  
          public class CountJobListener2 : IJobListener
          {
              public string Name =&gt; throw new NotImplementedException();
  
              public Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
              {
                  throw new NotImplementedException();
              }
  
              public Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
              {
                  throw new NotImplementedException();
              }
  
              public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken))
              {
                  throw new NotImplementedException();
              }
          }
  
          public class SomethingToDo
          {
              public int CreateRandomNumbers()
              {
                  return new Random().Next(1, 101);
              }
          }
  </code></pre>
  
  <p>If anyone has better idea, plase submit.</p>