VS2017用SetTimer和多线程实现定时器

写一个例子时弄的,网上说精度不太行


#include "stdafx.h"
#include 
#include 

using namespace std;

void CALLBACK TimeProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime);//定时器的
DWORD WINAPI Thread1(LPVOID lpParameter);  //在你新开线程中执行的函数

int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE handle = CreateThread(NULL, 0, Thread1, NULL, 0, NULL);

	//写主线程的东西
	return 0;
}

int ncount = 0;
void CALLBACK TimeProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime)
{
	cout << ncount++ << endl;
}

DWORD WINAPI Thread1(LPVOID lpParameter)
{
	//SetTimer发送WM_TIMER消息到窗口消息环,并由消息环完成处理。
        //x64必须要强转,win32位不用
	SetTimer(NULL, 1, 1000, (TIMERPROC)TimeProc);
	//下面的while循环必须要
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (msg.message == WM_TIMER)
		{
			DispatchMessage(&msg);
		}
	}

	return 0;
}

 

 

你可能感兴趣的:(vs)