如何在 c++ 中每天的某个时间调用一个函数?

发布于 2024-10-07 18:04:18 字数 127 浏览 0 评论 0原文

好吧,我需要弄清楚如果我的应用程序正在运行,如何在某个时间调用函数。

它可能会在一个 while 循环中检查时间,但我不希望它每分钟检查一次时间,但我不希望它错过确切的时间,比如 18:00。

有什么办法吗?

Well i need to figure out how to call a function at a certain time if my application is running.

it will probably be in a while loop and check the time but i don't want it to check the time every minute but i don't want it to miss the exact time, say 18:00.

is there any way do to this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

裂开嘴轻声笑有多痛 2024-10-14 18:04:18

如果您想从已运行的程序中调用特定函数,请使用 Boost 的 日期时间库和Asio

#include <boost/date_time/gregorian/gregorian_types.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/asio.hpp>

namespace gregorian = boost::gregorian;
namespace posix_time = boost::posix_time;
namespace asio = boost::asio;

asio::io_service io_service_;
asio::deadline_timer timer_ (io_service_);

然后设定时间。这是使用所需时间字符串的示例,例如“18:00:00”。 (请注意,Boost 默认使用 UTC 作为时间戳!)

gregorian::date today = gregorian::day_clock::local_day();
posix_time::time_duration usertime = posix_time::duration_from_string(time)
posix_time::ptime expirationtime (today, usertime);

现在设置计时器来调用 foo()

timer_.expires_at (expirationtime);
timer_.async_wait (foo);    // use Boost::bind() if you want a member function

您可以使用以下命令向 timer_ 添加任意数量的计时器:上面两个代码片段。当一切准备就绪时:

io_service_.run ();

请注意,这将接管当前线程。因此,如果您希望程序的其余部分继续运行,则必须从不同的线程调用 run()。 (显然,请使用 Boost 的 Thread 库。)

If you want to call a specific function from within a program that's already running, use Boost's Date Time library and Asio.

#include <boost/date_time/gregorian/gregorian_types.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/asio.hpp>

namespace gregorian = boost::gregorian;
namespace posix_time = boost::posix_time;
namespace asio = boost::asio;

asio::io_service io_service_;
asio::deadline_timer timer_ (io_service_);

Then set the time. This is an example using a string of the desired time, like "18:00:00". (Note that Boost defaults to UTC for the timestamps!)

gregorian::date today = gregorian::day_clock::local_day();
posix_time::time_duration usertime = posix_time::duration_from_string(time)
posix_time::ptime expirationtime (today, usertime);

Now set the timer to call foo():

timer_.expires_at (expirationtime);
timer_.async_wait (foo);    // use Boost::bind() if you want a member function

You can add as many timers as you want to timer_ by using the above two code snippets. When everything is ready:

io_service_.run ();

Note that this will take over the current thread. So you must call run() from a different thread if you want the rest of your program to keep going. (Obviously, use Boost's Thread library for that.)

揪着可爱 2024-10-14 18:04:18

编辑:我不知道为什么我认为这是Windows。我没有看到任何地方有这样的说法。以下API信息仅适用于Windows,但原理是相同的。 Boost.Asio 有便携式异步计时器(如果不是 Windows)。

Windows 信息:当您的应用启动时,检查所需事件的剩余时间间隔并启动 定时器队列定时器在适当的时间弹出。计时器将在初始间隔后弹出,然后在定期间隔后重复弹出。

CreateTimerQueueTimer函数

创建计时器队列计时器。这
计时器在指定的到期时间到期
时间,然后在每个指定的时间之后
时期。当定时器到期时,
调用回调函数。

使这种事件驱动比定期检查时钟更有效。

EDIT: I don't know why I assumed this was Windows. I don't see anywhere that it says so. The API info below is only applicable for Windows but the principle is the same. Boost.Asio has portable async timers, if not Windows.

Windows Info : When your app starts up, check the interval remaining to your desired event and start a Timer Queue Timer to pop at the appropriate time. The timer will pop after an initial interval, and then repeatedly after a periodic interval.

CreateTimerQueueTimer Function

Creates a timer-queue timer. This
timer expires at the specified due
time, then after every specified
period. When the timer expires, the
callback function is called.

Much more efficient to make this event-driven than to periodically check the clock.

沉默的熊 2024-10-14 18:04:18

最好的方法是使用提供此功能的操作系统工具,而您只需担心编写函数即可。但如果这不是一个选择,您将需要按照您所说的定期检查时钟。

The best way would be to use the operating system tools that provide this and you only worry about writing your function. But if thats not an option you will need to just check the clock periodicly as you said.

饮惑 2024-10-14 18:04:18

您可以在系统中安装计划任务来通知您的应用程序。
例如,如果您的系统是 Linux,则可以使用 cron。

You could install a scheduled task in your system that will notify your application.
If your system is Linux you can use cron for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文