2

I need to trigger a job each hour by starting a specific hour. For example, it starts at 10:00 pm and runs at 11:00 pm, 00:00, 01:00 am, 02:00 am (and so on).

I already tried two options but the job is not triggered.

//second try

    DateTime now = DateTime.Now;
            DateTime dt = new DateTime(now.Year, now.Month, now.Day, Hour, Minutes 0);
            trigger = TriggerBuilder.Create()
            .WithIdentity("job1", "JobExample")
            .WithSimpleSchedule(x => x
                .WithIntervalInHours(1))
             .StartAt(new DateTimeOffset(dt))

//first try

        trigger = TriggerBuilder.Create()
        .WithIdentity("job1", "JobExample")
        .WithDailyTimeIntervalSchedule( x => x.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(Hour, Minutes))
         .WithIntervalInHours(1))
walen
  • 7,103
  • 2
  • 37
  • 58
  • 3
    `00:00, 00:01am` ...these are not 1 hour apart. Is it a typo? And what does "doesn't work" mean. The job is not triggered? It triggers but at the wrong intervals? You get errors? Please clarify – ADyson Mar 15 '19 at 10:49
  • What exactly doesn't work? The trigger is just being created its calling . Build() at the end. You can try this tool https://github.com/guryanovev/CrystalQuartz to easily see with what scheudle the trigger is being built – npo Mar 15 '19 at 10:53
  • Possible duplicate of [How to call a method daily, at specific time, in C#?](https://stackoverflow.com/questions/3243348/how-to-call-a-method-daily-at-specific-time-in-c) – Thewickedislick Mar 15 '19 at 10:54
  • Sorry, I already updated my question –  Mar 15 '19 at 11:15
  • The jobs are triggered, but not when I need. For example, in first try the job started at 22pm (for example), it was triggered at 23 pm and notihing more. In the next day started again at 22pm. In second try, the job does not triggered in the next hour... –  Mar 15 '19 at 11:17

2 Answers2

2

I think you missing

.RepeatForever()

trigger = TriggerBuilder.Create()
.WithIdentity("trigger8") // because group is not specified, "trigger8" will be in the default group
.StartAt(DateBuilder.EvenHourDate(null)) // get the next even-hour (minutes and seconds zero ("00:00"))
.WithSimpleSchedule(x => x
    .WithIntervalInHours(2)
    .RepeatForever())
// note that in this example, 'forJob(..)' is not called 
//  - which is valid if the trigger is passed to the scheduler along with the job  
.Build();

scheduler.scheduleJob(trigger, job);

simpletriggers

Jophy job
  • 1,924
  • 2
  • 20
  • 38
  • Thank you, but I need that trigger start a specific hour:minute and then, repeat each hour:minute (forever) –  Mar 15 '19 at 13:01
  • that's possible , just add ".RepeatForever()" in your trigger and try .TriggerBuilder.Create().WithIdentity("job1", "JobExample").WithSimpleSchedule(x => x.WithIntervalInHours(1).RepeatForever()).StartAt(new DateTimeOffset(dt)); – Jophy job Mar 15 '19 at 13:54
  • I will try ! Thank you ! :) –  Mar 15 '19 at 14:02
0

Try use chron expression.

trigger = TriggerBuilder.Create()
    .WithIdentity("trigger3", "group1")
    .WithCronSchedule("0 0 0,1,2,3,4,5,6,7,8,9,10,11,22,23 ? * * *")
    .ForJob("myJob", "group1")
    .Build();

Cronmaker Freeformatter cron expression

W GDJ
  • 36
  • 3