Edalat Feizi Edalat Feizi - 3 months ago 24
ASP.NET (C#) Question

How to schedule a job to run 10 sec later in quartz.net?

I have a scheduled job that will fire every 1 minute, how can I schedule a job to run 10 sec later when added to scheduler?

var jobKey = new JobKey("firstJob", "jobGroup");

var firstJob = JobBuilder.Create<MatchJob>()
.WithIdentity("firstJob")
//.SetJobData()
.WithIdentity(jobKey)
.Build();

var firstTrigger = TriggerBuilder.Create()
.WithIdentity("firstTrigger")
.StartNow()
//.StartAt(DateTime.Today.Hour)
.WithSimpleSchedule(x => x.WithIntervalInMinutes(1).RepeatForever())
.Build();

AddJobToScheduler(firstJob, firstTrigger);

Answer

What about doing it with a cron-expression looking something like this: 10 0/1 * * * ? *? This would fire with one minute intervals but 10 seconds after your other job.

To add a cron-expression simply use .WithCronSchedule("10 0/1 * * * ? *") on your trigger.

The cron expression for your current job would be 0 0/1 * * * ? *. For a good tutorial on cron-expressions check this out!

Edit

If what you want is a job that is fired 10 seconds after its added to scheduler you could just do .StartAt(DateTime.Now.AddSeconds(10))