zoukankan      html  css  js  c++  java
  • VC++控制台程序中使用定时器

     我现在项目是一个控制台程序,用到的Win32API都是与界面无关的,今天需要加入定时器刷新的功能,由于没有消息循环,所以WM_TIMER消息应该如何处理呢?综合了下网上找到的资料,写了个简单的demo,个人以为这种在一个线程中创建定时器,再通过指定的回调函数来处理定时器触发的模式是比较好的。


    #include   <windows.h>   
    #include   <stdio.h>   
    #include   <conio.h>   

    int   count   =0;   

    VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
    {
        count++;   
        printf("WM_TIMER   in   work   thread   count=%d\n",count);   
    }

    DWORD CALLBACK   Thread(PVOID   pvoid)   
    {   
        MSG  msg;   
        PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);   
        UINT  timerid=SetTimer(NULL,111,3000,(TIMERPROC)TimerProc);   
        BOOL  bRet;   
        
        while(   (bRet = GetMessage(&msg,NULL,0,0))!=   0)   
        {     
            if(bRet==-1)   
            {   
                //   handle   the   error   and   possibly   exit   
            }   
            else   
            {    
                TranslateMessage(&msg);     
                DispatchMessage(&msg);     
            }   
        }   
        KillTimer(NULL,timerid);   
        printf("thread   end   here\n");   
        return   0;   
    }

    int    main()   
    {   
        DWORD   dwThreadId;   
        printf("use   timer   in   workthread   of   console   application\n");   
        HANDLE   hThread  =    CreateThread(NULL,0,Thread,0,0,&dwThreadId);
        _getch(); 
        return 0;
    }   
  • 相关阅读:
    spring @Primary-在spring中的使用(十九)
    Java中lombok @Builder注解使用详解(十八)
    Spring Boot的MyBatis注解:@MapperScan和@Mapper(十七)
    js基础只是总结-语句
    js基础知识-数据类型
    启动redis服务报错Creating Server TCP listening socket *:6379: bind: Address already in use [duplicate]
    gitlab 配置SSH和ACCESS TOKEN
    https nginx配置
    Vue和React区别
    深入虚拟DOM和DOM-diff
  • 原文地址:https://www.cnblogs.com/winkyao/p/2355536.html
Copyright © 2011-2022 走看看