How to run something every t seconds in C?

In C, I want the statement block to execute several times every t seconds. How to do it?

+6
c timer
source share
4 answers

This cannot be done in standard C, you need to use some kind of API for a specific platform.

One popular choice is the POSIX alarm() function.

This is a "clean" asynchronous solution. Of course, you can measure and process time in other ways, but they still depend on the platform. You can use sleep() (again POSIX) to just block. If you combine this with your queries in a loop, this should work too.

+7
source share

You will need to create a thread or process that starts a loop containing a wait request.

+3
source share

If your application runs on Windows, you can use the SetTimer function instead .

+1
source share

Accept the shutdown recommendation for the alarm (), but be sure to set another alarm () after you finish with the batch block.

0
source share

All Articles