Quartz C#每小时运行一次

时间:2019-03-15 10:45:30

标签: c# quartz.net

我需要每个小时通过开始一个特定的小时来触发工作。例如,它从10:00 pm开始,并在11:00 pm,00:00、01:00 am,02:00 am(依此类推)运行。

我已经尝试了两个选项,但未触发作业。

//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))

2 个答案:

答案 0 :(得分:0)

我认为您想念

  

.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

答案 1 :(得分:0)

尝试使用chron表达式。

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