c++ Timer使用总结

清泛原创
窗口应用程序使用Timer
#define TIMER_ID 1000  //定时器ID,可任意。触发后回调函数中用于区别不同的定时器以执行不同的任务
SetTimer(TIMER_ID, 1000 , NULL);  //启动定时器,1秒后触发
KillTimer(TIMER_ID);  //取消定时器

//.h文件函数申明
afx_msg void OnTimer(UINT_PTR nIDEvent);

//.cpp函数定义
void CxxDlg::OnTimer(UINT_PTR nIDEvent)
{
  switch (nIDEvent)
  {
    case TIMER_ID:
    {
      //do something
    }
    break;
  default:
    break;
  }
}

//定义消息宏,这个非常重要,否则触发后不会调用OnTimer()函数
BEGIN_MESSAGE_MAP(CTradeMonitorView, CView)
...
ON_WM_TIMER()
...
END_MESSAGE_MAP()

控制台程序使用Timer:
#include  <windows.h>   
#include  <iostream>   

VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)   
{   
	std::cout << "hello " << std::endl;
}   

void main()   
{   
	int   timer1 = 1;
	MSG   msg;

	SetTimer(NULL, timer1, 5000, TimerProc);
	int   itemp;
	while ( (itemp = GetMessage(&msg,NULL,NULL,NULL)) && (itemp!=0) &&  (-1 != itemp) )   
	{     
		if (msg.message == WM_TIMER)     
		{     
			std::cout << "i got the message " << std::endl;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}   
	}
}



多线程使用Timer:
#include <stdafx.h>
#include <windows.h> 
#include <stdio.h>
#include <conio.h>

unsigned long WINAPI Thread(PVOID pvoid);
void main()
{    
	DWORD dwThreadId;
	printf("use timer in workthread of console application<masterz>\n");
	HANDLE   hThread = CreateThread(
		NULL,                              //   no   security   attributes
		0,                                 //   use   default   stack   size
		Thread,                            //   thread   function
		0,                                 //   argument   to   thread   function
		0,                                 //   use   default   creation   flags
		&dwThreadId);  
	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);
	UINT   timerid = SetTimer(NULL,111,3000,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) 
		{
			count++;
			printf("WM_TIMER in work thread count=%d\n", count);
			if(count > 4)
				break;
		}
		else
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	KillTimer(NULL, timerid);
	printf("thread end here\n");
	return 0;
}



定时器的原型是:
WINUSERAPI UINT WINAPI SetTimer(HWND hWnd , UINT nIDEvent, UINT uElapse, TIMERPROC lpTimerFunc);

    hWnd      是欲设置定时器的窗体句柄。定时时间到时,系统会向该窗体发送WM_TIMER消息。

    nIDEvent      定时器标识符。在一个窗体内可以使用多个定时器,不同的定时器根据nIDEvent来区分。

    uElapse         定时时间,单位是毫秒。

    lpTimerFunc 定时器的回调函数。如果该值为NULL,定时时间到时,定时器发送的消息WM_TIMER由窗体映像该消息的函数处理;否则由回调函数处理,说白一点,这里的回调函数就是取代OnTimer的处理函数。

c++ Timer WM_TIMER OnTimer 总结

分享到:
评论加载中,请稍后...
创APP如搭积木 - 创意无限,梦想即时!
回到顶部