c语言怎么实现多个计时器?且要互不干扰下面是单个计时器程序 - 爱问答

(爱问答)

c语言怎么实现多个计时器?且要互不干扰下面是单个计时器程序

c语言怎么实现多个计时器?且要互不干扰下面是单个计时器程序

真正的定时器,不能靠循环的,它不稳定且Sleep占CPU的且更不可能互不干扰

它要用系统的功能,可用系统本身定时器或多线程实现,以下是windows下两个不同定时器的实现参考

#include <windows.h> #include <stdio.h> #pragma link "user32.lib" static BOOL Exit1 = FALSE; static BOOL Exit2 = FALSE; const  UINT Timer1 = 10; const  UINT Timer2 = 11; VOID CALLBACK Timer1Fun( HWND h, UINT u1, UINT u2, Dword d ) {    static int count=120; //120秒,2分种    if (count==0) {        printf("时间1到!!! ");        Exit1 = TRUE;        return;    }    if (count%5==0 || count<5) { //5秒一输出        printf("定时器1:%d秒 ",count);    }    count--; } VOID CALLBACK Timer2Fun( HWND h, UINT u1, UINT u2, Dword d ) {    static int count=120; //120秒,2分种    if (count==0) {        printf("时间2到!!! ");        Exit2 = TRUE;        return;    }    if (count%10==0 || count<10) { //10秒一输出        printf("定时器2:%d秒 ",count);    }    count--; } int main() {    MSG msgFoo;    SetTimer( NULL, Timer1, 1000, Timer1Fun );    //1秒一次    SetTimer( NULL, Timer2, 1000, Timer2Fun );    //1秒一次    while( !(Exit1 && Exit2) && GetMessage( &msgFoo, NULL, 0, 0 ) ) {        TranslateMessage( &msgFoo );        DispatchMessage( &msgFoo );    }    KillTimer( NULL, Timer1 );    KillTimer( NULL, Timer2 );        return 0; }


相关标签:c语言

下一篇:bat怎么完成对注册表的操作

上一篇:c语言判断三角形的类型,如果只输入一条边,则提示错误这个命令怎么做啊

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?