我做了一个简单的网络监控系统,我希望它每隔一个小时运行一次,以保持对客户端系统的连续跟踪。谁能告诉我如何让我的代码每隔一个小时执行一次。
编辑:
我的平台是windows-7,我使用的是Visual Studio 2010。
发布于 2013-11-15 10:10:23
在Linux上,尝试一个cron作业。这会安排程序以固定的时间间隔运行。
http://www.unixgeeks.org/security/newbie/unix/cron-1.html
发布于 2013-11-15 10:52:34
Windows Task Scheduler的应用编程接口文档是here。它不是最简单的应用编程接口,命令行工具schtasks.exe可能是一个更简单的解决方案。
发布于 2013-11-15 19:13:12
查看Waitable Timer Objects和Using Waitable Timer Objects以深入了解合适的定时器API。SetWaitableTimer function允许将周期设置为3,600,000 ms,表示所需的一小时周期。
示例:
#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hTimer = NULL;
LARGE_INTEGER liDueTime;
liDueTime.QuadPart = -100000000LL;
// due time for the timer, negative means relative, in 100 ns units.
// This value will cause the timer to fire 10 seconds after setting for the first time.
LONG lPeriod = 3600000L;
// one hour period
// Create an unnamed waitable timer.
hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
if (NULL == hTimer)
{
printf("CreateWaitableTimer failed, error=%d\n", GetLastError());
return 1;
}
printf("Waiting for 10 seconds...\n"); // as described with liDueTime.QuadPart
if (!SetWaitableTimer(hTimer, &liDueTime, lPeriod , NULL, NULL, 0))
{
printf("SetWaitableTimer failed, error=%d\n", GetLastError());
return 2;
}
// and wait for the periodic timer event...
while (WaitForSingleObject(hTimer, INFINITE) == WAIT_OBJECT_0) {
printf("Timer was signaled.\n");
// do what you want to do every hour here...
}
printf("WaitForSingleObject failed, error=%d\n", GetLastError());
return 3;
}https://stackoverflow.com/questions/19992112
复制相似问题