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.
Asked
Active
Viewed 1,798 times
-2
-
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 Answers
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
-
-
-
2@POW You have all the seconds you need. `time()` returns the number of seconds passed since 1 January 1970. – Jocke Aug 05 '13 at 12:27
-
Sleep will wait until tresh time has elapsed. But if `Myfunction` has taken more time than usual, how can I shorten the waiting time? – Explicat Aug 05 '13 at 14:06