0

My requeriment is to build a Quartz.net Task that:

  • Runs at specified time for a variable amount of minutes and then finish?

I have this class:

    public class Proccess
    {
        public static void Start()
        {
            Console.WriteLine("I'm starting");
        }

        public static void End()
        {
            Console.WriteLine("I'm finishing");
        }
    }

Is there any way to configure a job with Quartz.Net to call Process.Start() wait for X minutes and the call Process.End() ?

Maybe somethings like this?

public class TapWaterJob : IJob
{
    public TapWaterJob()
    {
        // quartz requires a public empty constructor so that the
        // scheduler can instantiate the class whenever it needs.
    }

    public void Execute(IJobExecutionContext context)
    {
        Proccess.Start();
        System.Threading.Thread.Sleep(10000);
        Process.End();
    }
}
Marc
  • 2,023
  • 4
  • 16
  • 30
  • "Runs at specified time for a variable amount of minutes and then finish " not clear how often it should be triggered – Valentin Jan 12 '16 at 22:29
  • It will run only once – Marc Jan 12 '16 at 22:33
  • Possible duplicate of [Configuring Quartz.Net to stop a job from executing, if it is taking longer than specified time span](http://stackoverflow.com/questions/24446215/configuring-quartz-net-to-stop-a-job-from-executing-if-it-is-taking-longer-than) – Najera Feb 06 '16 at 07:52

1 Answers1

0

If you just want to run something to completion, it seems like just letting object stop on it's own is the right way to do it (i.e. call stop for itself).

If you want Quartz to invoke the stop, you could do something like this:

var jobMap = new JobDataMap();
jobMap.Put("process", process);

var startJob = JobBuilder.Create<StartJob>()
    .WithIdentity("startJob")
    .SetJobData(jobMap).Build();
ITrigger startTrigger = TriggerBuilder.Create()
    .WithIdentity("startTrigger")
    .StartNow()
    .Build();
var stopJob = JobBuilder.Create<StopJob>()
    .WithIdentity("stopJob")
    .SetJobData(jobMap).Build();
ITrigger stopTrigger = TriggerBuilder.Create()
    .WithIdentity("stopTrigger")
    .StartAt(DateTime.Now.AddMinutes(5))
    .Build();

var sched = StdSchedulerFactory.GetDefaultScheduler();
sched.Start();
sched.ScheduleJob(startJob, startTrigger);
sched.ScheduleJob(stopJob, stopTrigger);

Replace the 5 parameter to StartAt with a variable with the number of minutes you want to wait.

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98