首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在特定时间后执行代码

在特定时间后执行代码
EN

Stack Overflow用户
提问于 2013-11-15 09:55:15
回答 3查看 160关注 0票数 0

我做了一个简单的网络监控系统,我希望它每隔一个小时运行一次,以保持对客户端系统的连续跟踪。谁能告诉我如何让我的代码每隔一个小时执行一次。

编辑:

我的平台是windows-7,我使用的是Visual Studio 2010。

EN

回答 3

Stack Overflow用户

发布于 2013-11-15 10:10:23

在Linux上,尝试一个cron作业。这会安排程序以固定的时间间隔运行。

http://www.unixgeeks.org/security/newbie/unix/cron-1.html

票数 1
EN

Stack Overflow用户

发布于 2013-11-15 10:52:34

Windows Task Scheduler的应用编程接口文档是here。它不是最简单的应用编程接口,命令行工具schtasks.exe可能是一个更简单的解决方案。

票数 1
EN

Stack Overflow用户

发布于 2013-11-15 19:13:12

查看Waitable Timer ObjectsUsing Waitable Timer Objects以深入了解合适的定时器API。SetWaitableTimer function允许将周期设置为3,600,000 ms,表示所需的一小时周期。

示例:

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19992112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档