0

I made a simple network monitoring system and I want it to run after every one hour to keep a continuous track of the client system. Can anyone tell me how I can make my code to execute after every one hour.

EDITED:

My platform is windows-7 and i am using Visual Studio 2010.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Saad Saadi
  • 1,031
  • 10
  • 26
  • 1
    As you've now noticed, when you ask half a question, you get answers that are not appropriate. If you are working on Windows, say so — you will get different answers that way, ones that are more appropriate to Windows than Unix. – Jonathan Leffler Nov 15 '13 at 03:00

3 Answers3

1

On Linux, try a cron job. This schedules programs to run at regular intervals.

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

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
  • Thanks for the suggestions but with due respect, i am working on windows-7...with Visual Studio 2010. – Saad Saadi Nov 15 '13 at 02:11
  • Surely there is some [Windows analog](http://stackoverflow.com/questions/638124/cron-like-system-for-windows) to `cron`. – Duck Nov 15 '13 at 02:12
  • By searching a program analogous to **cron**, i found [At](http://ss64.com/nt/at.html) and also **cronw**..(its not working with newer version of windows).. – Saad Saadi Nov 15 '13 at 02:22
  • but i want to do it using a program written in C...can it be done? – Saad Saadi Nov 15 '13 at 02:23
  • Yes. You will have to use whatever Timer facilities Windows provides in its API. – Duck Nov 15 '13 at 02:31
1

The API documentation for the Windows Task Scheduler is here. It is not the simplest API to work with, and the command line tool schtasks.exe might be a simpler solution.

david25272
  • 976
  • 6
  • 12
0

look into Waitable Timer Objects and Using Waitable Timer Objects to get insight into a suitable timer API. The SetWaitableTimer function allows to set a period to 3,600,000 ms, which represents the desired one hour period.

Example:

#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;
}
Arno
  • 4,994
  • 3
  • 39
  • 63