您的位置:首页 > 编程语言 > C语言/C++

VC++控制台程序中使用定时器

2016-03-04 10:03 507 查看
#include   <windows.h>  

#include   <stdio.h>  

#include   <conio.h>  

int   count=0;  

VOID __stdcall TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)

{

    count++;   

    printf("WM_TIMER   in   work   thread   count=%d\n",count);  

 

 if (count==10)

 {

  count=0;

  system("cls");

 }

}

DWORD CALLBACK   Thread(PVOID   pvoid)  

{  

    MSG  msg;  

    PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);  

    UINT  timerid=SetTimer(NULL,111,3000,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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: