c-是否有某种命令用于在程序中创建延迟



许多人告诉我,有一种更好的方法可以为X创建毫秒数的延迟。人们告诉我睡眠命令和usleep((,但我没能做到。

目前我正在使用这个:

void delay(unsigned int mseconds) {
clock_t goal=mseconds+clock();
while(goal>clock());
}

通过这样做

delay(500);
printf("Hello there");

我可以让文本在半秒钟后出现,但我想找到一种更好的方法来做到这一点,因为人们告诉我这是一种糟糕的方法,而且可能不太准确。

我想明白了!!!天哪,我第一次尝试让sleep()命令工作时被甩了

#include<stdio.h>
#include<windows.h>
int main(){
int load;
int Loadwait=100
for(load=0;load<100;load+=5){
printf("Loading %d",load);
Sleep(Loadwait);
system("cls");
}
return 0;
}

使用musl-libc,您应该能够使用thrd_sleep((。

#include <threads.h>
#include <time.h>
#include <stdio.h>
int main(void)
{
printf("Time: %s", ctime(&(time_t){time(NULL)}));
thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec
printf("Time: %s", ctime(&(time_t){time(NULL)}));
}

最新更新