首页 > 代码库 > C/C++ 学习之 定时器

C/C++ 学习之 定时器

下面是两个简单的例子,说明在  windows  控制台应用程序中定时器的用法

1、直接声明回调函数,然后在回调函数中处理消息。

// programe for timer

#include "stdio.h"
#include "conio.h"
#include <Windows.h>

int count = 0;

void CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
	printf("WM_TIMER in work thread count = %d\n",count++);
}

int main()
{
	SetTimer (NULL, 0, 1000, TimerProc);
	MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
		if(msg.message==WM_TIMER)
		{
			DispatchMessage(&msg);
			if(count == 7)
			{
				printf("should stop!\n");
				break;
			}
		}
    }
	KillTimer (NULL, 0);
	return 0;
}

2、建立线程,在线程中建立消息处理机制

//programe for timer
#include   <windows.h>  
#include   <stdio.h>  
#include   <conio.h>  

unsigned   long   WINAPI   Thread(PVOID   pvoid); 

void   main()  
{  
    DWORD   dwThreadId;  
    printf("控制台应用程序:线程-定时器\n");  
    HANDLE hThread = CreateThread(NULL, 0, Thread, 0, 0, &dwThreadId);
	//安全等级,默认栈空间,线程名,线程参数,编程标志,线程ID       
            
    DWORD   dwwait = WaitForSingleObject(hThread,1000*30);  
    switch(dwwait)
    {  
    case   WAIT_ABANDONED:  
        printf("main thread WaitForSingleObject return WAIT_ABANDONED\n");  
        break;  
    case   WAIT_OBJECT_0:  
        printf("main thread WaitForSingleObject return WAIT_OBJECT_0\n");  
        break;  
    case   WAIT_TIMEOUT:  
        printf("main thread WaitForSingleObject return WAIT_TIMEOUT\n");  
        break;  
    }  
    CloseHandle(hThread);  
    getch();  
}  

unsigned  long   WINAPI   Thread(PVOID   pvoid)  
{  
    MSG   msg;
    PeekMessage(&msg,   NULL,   WM_USER,   WM_USER,   PM_NOREMOVE);
	//该函数为一个消息检查线程消息队列,并将该消息(如果存在)放于指定的结构。
	//接收消息的 MSG 结构指针,句柄,指定被检查的消息范围里的第一个消息,最后一个,确定消息如何被处理
	//PM_NOREMOVE,PeekMessage处理后,消息不从队列里除掉
	//PM_REMOVE,PeekMessage处理后,消息从队列里除掉
	//PM_NOYIELD,此标志使系统不释放等待调用程序空闲的线程
    UINT   timerid = SetTimer(NULL,111,1000,NULL);  
    BOOL   bRet;  
    int   count = 0;  
    while((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)  
    {    
        if(bRet == -1)  
        {  
            //   handle   the   error   and   possibly   exit  
        }  
        else if(msg.message == WM_TIMER) 
        {  
            printf("WM_TIMER in work thread count = %d\n",count++);  
            if(count>4)  break;  
        }  
        else  
        {  
            TranslateMessage(&msg);   
			//将虚拟键消息转换为字符消息,下一次线程调用函数GetMessage或PeekMessage时被读出。
            DispatchMessage(&msg);   
			//该函数分发一个消息给窗口程序,消息传递给操作系统,然后操作系统去调用我们的回调函数
        }  
    }  
    KillTimer(NULL,timerid);  
    printf("thread  end  here\n");  
    return   0;  
}


C/C++ 学习之 定时器