windows 如何在 C++ 中安排任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4266078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to schedule a task in C++
提问by subs
I have a C++ windows service which should call a function every 15 minutes. I am new to c++ and have no idea how to do this.Can anybody tell me how to achieve this?
我有一个 C++ windows 服务,它应该每 15 分钟调用一个函数。我是 C++ 新手,不知道如何做到这一点。有人能告诉我如何做到这一点吗?
Thanks, Subrat
谢谢,苏布拉特
回答by Nate
Call SetTimer()
.
打电话SetTimer()
。
Alternately, use the Task Scheduler API, which can execute a regular .exe
or COM handler on a predefined schedule. If you do that, you may not even need a service.
或者,使用Task Scheduler API,它可以.exe
按预定义的计划执行常规或 COM 处理程序。如果您这样做,您甚至可能不需要服务。
回答by peoro
There are many libraries to provid timers.
有很多库可以提供计时器。
What operating system and what libraries are you using? You could implement one by yourself using Boost::Timer(or even standard C time-related functions from ctime
header), or, if you're using a library that handles the main loop, it for sure provides some functions.
您使用的是什么操作系统和什么库?您可以使用Boost::Timer(甚至来自ctime
标头的标准 C 时间相关函数)自己实现一个,或者,如果您使用的是处理主循环的库,它肯定会提供一些功能。
回答by Armen Tsirunyan
There is <ctime>
header in C++ (<time.h>
in C) which provides some basic low-level time functionality. However if you are seeking something more close to c#'s timer then you can use Qt's QTimer
class. Get to know Qt- it's a good thing.
有<ctime>
在C头++(<time.h>
C)中,其提供了一些基本的低级时间功能。但是,如果您正在寻找更接近 c# 的计时器的东西,那么您可以使用 Qt 的QTimer
类。了解Qt- 这是一件好事。