-2

Is it possible to create something like a trigger in C which calls a method, say every five seconds? It would even be better if the timer goes on while the method is being carried out.

Explicat
  • 1,075
  • 5
  • 16
  • 40
  • You might want to have a look at [this](http://stackoverflow.com/questions/1784136/simple-signals-c-programming-and-alarm-function). – Nobilis Aug 05 '13 at 11:36

2 Answers2

2

You can use the alarm() function , but it's not available on all platforms (Windows for example).

Unknown
  • 5,722
  • 5
  • 43
  • 64
1

How about this:

long thresh = 5*1000; //mlliseconds
//Implement getcurTime()
int prev_time = getcurTime() - thresh;

while(1){

    //Time Elapsed ?
    if (getcurTime() - prev_time >= thresh){
        prev_time = getcurTime();
        Myfunction();
    }
    Sleep(thresh);
}
P0W
  • 46,614
  • 9
  • 72
  • 119