2013-11-15 139 views
0

我做了一个简单的网络监控系统,我希望它在每个小时后运行以保持客户端系统的连续跟踪。任何人都可以告诉我如何让我的代码在每一小时后执行。在特定时间后执行代码

编辑:

我的平台是Windows-7和我使用Visual Studio 2010中

+1

正如你现在已经注意到了,当你问半个问题,你得到的是不恰当的答案。如果您正在使用Windows,请这么说 - 您将以这种方式得到不同的答案,这比Windows更适合Windows。 –

回答

1

在Linux上,尝试cron工作。这安排程序定期运行。

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

+0

感谢您的建议,但值得尊重的是,我正在使用Visual Studio 2010开发windows-7 ... –

+0

当然,有些[Windows模拟](http://stackoverflow.com/questions/638124/cron-like - windows系统)转换为'cron'。 – Duck

+0

通过搜索类似于** cron **的程序,我找到了[At](http://ss64.com/nt/at.html)以及** cronw ** ..(它不适用于较新版本的windows).. –

1

为Windows任务调度程序中的API文档here。这不是最简单的API,命令行工具schtasks.exe可能是一个更简单的解决方案。

0

调查Waitable Timer ObjectsUsing Waitable Timer Objects以洞察合适的计时器API。 SetWaitableTimer function允许将期间设置为3,600,000毫秒,其表示期望的一小时期间。

例子:

#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; 
}