先来看一个简单的程序, 实现一个定时器来定时调用test函数:
#include <iostream>
#include <windows.h>
using namespace std;
void test()
{
	cout << "test" << endl;
}
DWORD WINAPI ThreadFun(LPVOID pM)
{
	int timeVal = *(int *)pM;
	while(1)
	{
		test();
		Sleep(timeVal); // 3s调用一次test
	}
	return 0;
}
int main()
{
	int timeVal = 3000;
	HANDLE handle = CreateThread(NULL, 0, ThreadFun,  &timeVal, 0, NULL);
	CloseHandle(handle);
	while(1);
	return 0;
}
然而, 上述是有问题的, 没有考虑test函数本身的执行时间, 例如, 下面程序实际上是5秒执行一次test
#include <iostream>
#include <windows.h>
using namespace std;
void test()
{
	cout << "test" << endl;
	Sleep(2000);
}
DWORD WINAPI ThreadFun(LPVOID pM)
{
	int timeVal = *(int *)pM;
	while(1)
	{
		int start = GetTickCount();  
		test();
		Sleep(timeVal); // 实际是5s调用一次test
	}
	return 0;
}
int main()
{
	int timeVal = 3000;
	HANDLE handle = CreateThread(NULL, 0, ThreadFun,  &timeVal, 0, NULL);
	CloseHandle(handle);
	while(1);
	return 0;
}
改进后的程序为:
#include <iostream>
#include <windows.h>
using namespace std;
void test()
{
	cout << "test" << endl;
	Sleep(2000); // 假设此处不超过3s
}
DWORD WINAPI ThreadFun(LPVOID pM)
{
	int timeVal = *(int *)pM;
	while(1)
	{
		int start = GetTickCount();  
		test();
		
		while(GetTickCount() - start < timeVal)
		{
			Sleep(10);
		}
	}
	return 0;
}
int main()
{
	int timeVal = 3000;
	HANDLE handle = CreateThread(NULL, 0, ThreadFun,  &timeVal, 0, NULL);
	CloseHandle(handle);
	while(1);
	return 0;
}该程序是3s调用一次test函数。
         诚然, 上述程序也没有考虑一些特殊和异常的情况(比如test函数本身时间超过3s, 那代码就有问题了), 在本文中, 我们仅仅讨论一种可行的方法, 如果在具体项目中, 还需要更加完善更加健壮的代码。