【参考资料:http://blog.csdn.net/weiwenhp/article/details/8733216】
【使用MFC的情况下】
示例1,使用消息处理
新建一个MFC project,类型选Dialog,
先设定下多久
//TimerTestDlg.h///
afx_msg void OnTimer(UINT_PTR nIDEvent); //回调函数,这里的函数名OnTimer不能换成其他名字.固定的
TimerTestDlg.cpp//
#include "afxmt.h"
#define TIMERID 8 //指定一个timer的ID,你可以同时设定多个timer,就用ID来区分
CCriticalSection g_criSection; //【阿甘补充:在这里使用了一个关键区对象】
BEGIN_MESSAGE_MAP(CTimerTestDlg, CDialog)
ON_WM_TIMER()
END_MESSAGE_MAP()
BOOL CTimerTestDlg::OnInitDialog()
{
SetTimer(TIMERID,1000,0); //这里就相当于设定了timer,如果要停掉timer就是KillTimer(TIMERID)
}
void CTimerTestDlg::OnTimer(UINT_PTR nIDEvent)
{
g_criSection.Lock();
static int nShowNum = 1;
if( nIDEvent == 8) // 【阿甘补充:注意Timer序号的一一对应。否则无响应】
{