我写了一个小控制台应用程序,我希望它在循环(一段时间)再次开始之前暂停一定秒数。
我在 Windows 操作系统上工作。
最佳答案
在 Windows 上,执行此操作的函数是 Sleep
,它占用您要休眠的毫秒数。要使用 Sleep
,您需要包含 windows.h
。
在 POSIX 系统上,函数 sleep
(来自 unistd.h
)完成了这个:
unsigned int sleep(unsigned int seconds);
DESCRIPTION
sleep() makes the calling thread sleep until
seconds seconds have elapsed or a signal arrives which is not ignored.
如果被信号打断,则返回 sleep 的剩余时间。如果您使用信号,更强大的解决方案是:
unsigned int time_to_sleep = 10; // sleep 10 seconds
while(time_to_sleep)
time_to_sleep = sleep(time_to_sleep);
当然,这是假设您的信号处理程序只花费可忽略不计的时间。 (否则,这将延迟主程序比预期更长的时间)
关于c - 在 C 中等待/暂停一定秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9241025/