我每天早上8:00在一个vibe.d网络应用程序中运行一个任务。目前,我使用带有周期参数的setTimer函数作为true。但是这样的话,我无法精确地控制触发任务的时间。有什么简单的方法吗?
发布于 2017-04-11 18:10:33
谢天谢地,我就是这么做的。我计算下一个上午8点之前的时间,然后打电话给setTimer。下面是供进一步参考的代码:
void startDailyTaskAtTime(TimeOfDay time, void delegate() task) {
// Get the current date and time
DateTime now = cast(DateTime)Clock.currTime();
// Get the next time occurrence
DateTime nextOcc = cast(DateTime)now;
if (now.timeOfDay >= time) {
nextOcc += dur!"days"(1);
}
nextOcc.timeOfDay = time;
// Get the duration before now and the next occurrence
Duration timeBeforeNextOcc = nextOcc - now;
void setDailyTask() {
// Run the task once
task();
// Run the task all subsequent days at the same time
setTimer(1.days, task, true);
}
setTimer(timeBeforeNextOcc, &setDailyTask);
}https://stackoverflow.com/questions/43347079
复制相似问题