title: In Quartz.net, run one instance of a job at a time
body: <p>I have referred the following question but didn't help me to solve the issue.</p>
  
  <p><a href="https://stackoverflow.com/questions/5984220/in-quartz-net-is-there-a-way-to-set-a-property-that-will-only-allow-one-instance">In Quartz.NET is there a way to set a property that will only allow one instance of a Job to run?</a></p>
  
  <p><a href="https://github.com/quartznet/quartznet/issues/469" rel="nofollow noreferrer">https://github.com/quartznet/quartznet/issues/469</a></p>
  
  <p>For CronTrigger, Used the following in the scheduler <code>cs.WithMisfireHandlingInstructionDoNothing()</code>.</p>
  
  <p>Applied the following attribute in the <code>HelloJob</code>
  <br><code>DisallowConcurrentExecution</code>.</p>
  
  <p><strong>What happened with the code?</strong><br>
  In the Execute method, I have set the breaking point. Based on my code the execute method will execute in 10 seconds each.</p>
  
  <p>After hitting the first breaking point, I have waited for another 31 seconds. Then I have removed the breaking point and executed the code, based on my expectation, that should be execute only one time for another attempt.</p>
  
  <blockquote>
    <p>But the execute method executed 3 times (3*10 seconds) within another
    10 seconds.</p>
  </blockquote>
  
  <p>How to solve this?</p>
  
  <p>Scheduler code.</p>
  
  <pre><code>ISchedulerFactory schedFact = new StdSchedulerFactory();
  IScheduler sched = schedFact.GetScheduler();
  sched.Start();
  
  // define the job and tie it to our HelloJob class
  IJobDetail job = JobBuilder.Create&lt;HelloJob&gt;()
      .WithIdentity("myJob", "group1")
      .Build();
  
  // Trigger the job to run now, and then every 40 seconds
  ITrigger trigger = trigger = TriggerBuilder.Create()
      .WithIdentity("trigger3", "group1")
      .WithCronSchedule("0/10 * * * * ?",cs=&gt;cs.WithMisfireHandlingInstructionDoNothing())
      .ForJob("myJob", "group1")
      .Build();
  
  TriggerKey key = new TriggerKey("trigger3", "group1");
  sched.ScheduleJob(job, trigger);
  </code></pre>
  
  <p>Job execution code.</p>
  
  <pre><code>[DisallowConcurrentExecution]
  public class HelloJob : IJob
  {        
       public static int count = 1;
      public void Execute(IJobExecutionContext context)
      {
           Console.WriteLine(count+" HelloJob strted On." + DateTime.Now.ToString());
          if (count == 1)
              Thread.Sleep(TimeSpan.FromSeconds(30));
  
          Interlocked.Increment(ref count);
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/NQ5nH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NQ5nH.png" alt="enter image description here"></a></p>
  
  <p>====================================================================</p>
  
  <h1><strong>Solution</strong></h1>
  
  <p>No need to go for interlocked or manual management.</p>
  
  <p>Quartz is already designed like only finishes the first schedule, the next one starts.</p>
  
  <p>So we no need to worry about that it will run concurrently.</p>
  
  <p>For example (The people like me :-p), the scheduler scheduled for 10 mins.</p>
  
  <p>But if we copy the following code in the execute method, you can see that,
  On the first time, it will take 20 minutes to finish.
  On the second time, it will take 15 minutes to finish.</p>
  
  <p>In between there won't be next schedule starts after 10 mins over.</p>
  
  <pre><code>   var startTime = DateTime.UtcNow;
  
              if (count == 1)
              {
                  while (DateTime.UtcNow - startTime &lt; TimeSpan.FromSeconds(20))
                  {
                      // Execute your loop here...
                  }
              }
              else if (count &gt; 1)
              {
                  while (DateTime.UtcNow - startTime &lt; TimeSpan.FromSeconds(15))
                  {
                      // Execute your loop here...
                  }
              }
              count++;
  </code></pre>