gpt4 book ai didi

quartz-scheduler - 使用 Quartz.NET 安排任务每天在特定时间运行

转载 作者:行者123 更新时间:2023-12-05 00:29:24 24 4
gpt4 key购买 nike

我每天在特定时间使用 Quartz.NET 执行任务,这是我的代码:

public class TestSchedule : ISchedule
{
public void Run()
{

DateTimeOffset startTime = DateBuilder.FutureDate(2, IntervalUnit.Second);

IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("job1")
.Build();

ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1")
.StartAt(startTime)
.WithDailyTimeIntervalSchedule(x => x.OnEveryDay().StartingDailyAt(new TimeOfDay(7, 0)).WithRepeatCount(0))
.Build();

ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sc = sf.GetScheduler();
sc.ScheduleJob(job, trigger);

sc.Start();
}
}

我的代码正在运行,但问题是它只能运行一次(似乎是因为 WithRepeatCount(0))
现在,怎么能说每天七点钟运行呢?
PS:我不想使用 CronTrigger要做到这一点。

最佳答案

DailyTimeIntervalTriggerImpl仅支持重复计数 .

This trigger also supports "repeatCount" feature to end the trigger fire time after a certain number of count is reached. Just as the SimpleTrigger, setting repeatCount=0 means trigger will fire once only! Setting any positive count then the trigger will repeat count + 1 times. Unlike SimpleTrigger, the default value of repeatCount of this trigger is set to REPEAT_INDEFINITELY instead of 0 though.



Cron 表达式很漂亮,还有很多 tools这可以帮助您实现您正在寻找的目标。

另一种选择是使用 SimpleTriggerImpl并每 24 小时设置一次间隔:
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1")
.StartAt(startTime)
.WithSimpleSchedule(x => x.RepeatForever().WithIntervalInHours(24))
.Build();

关于quartz-scheduler - 使用 Quartz.NET 安排任务每天在特定时间运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17289568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com