c - 在 C 中等待/暂停一定秒数

标签 c windows

我写了一个小控制台应用程序,我希望它在循环(一段时间)再次开始之前暂停一定秒数。

我在 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/

相关文章:

python - 无法导入 python 扩展

windows - 为什么对 HKEY_CURRENT_USER 注册表配置单元的 32 位 View 的访问会回退到 64 位 View ?

c - 返回指向具有单指针或双指针的结构数组的指针

C: 将 "static const char * const"分配给 "static const char *"

谁能解释一下这个C密码哈希算法吗?

c - libc.a 包含哪些标准库函数?

c - WBINVD指令使用

windows - Windows 上的任务和 -gnatP 开关

css - 使用粗体的 Arial 字体的意外边距

linux - 在斯卡拉 : not able to fin a class in Linux but in Windows for forName