开发者

How to run something every t seconds in C?

In C, I want a block of statements to be executed repeatedly every t seconds. How do I do th开发者_StackOverflow社区is?


This cannot be done in standard C, you need to use some platform-specific API.

One popular choice is POSIX' alarm() function.

This is a "pure" aynchronuous solution. It's of course possible to measure and handle time in other ways, but they're still platform-dependent. You could use sleep() (again, POSIX) to just block. If you combine that with your desired statements in a loop, that should work too.


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


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


Agree with unwind's recommendation for alarm() but don't forget to set another alarm() after you are done with the periodic processing block.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜