vc控制台程序中使用定时器
时间:2010-10-16 来源:lbsx
在新线程中创建定时器,指定回调函数,并且在线程中加入消息处理。
1
2 #include <windows.h>
3 #include <stdio.h>
4 #include <conio.h>
5
6 int count =0;
7
8 VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
9 {
10 count++;
11 printf("WM_TIMER in work thread count=%d\n",count);
12 }
13
14 DWORD CALLBACK Thread(PVOID pvoid)
15 {
16 MSG msg;
17 PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);
18 UINT timerid=SetTimer(NULL,111,3000,TimerProc);
19 BOOL bRet;
20
21 while( (bRet = GetMessage(&msg,NULL,0,0))!= 0)
22 {
23 if(bRet==-1)
24 {
25 // handle the error and possibly exit
26 }
27 else
28 {
29 TranslateMessage(&msg);
30 DispatchMessage(&msg);
31 }
32 }
33 KillTimer(NULL,timerid);
34 printf("thread end here\n");
35 return 0;
36 }
37
38 int main()
39 {
40 DWORD dwThreadId;
41 printf("use timer in workthread of console application\n");
42 HANDLE hThread = CreateThread(NULL,0,Thread,0,0,&dwThreadId);
43 _getch();
44 return 0;
45 }
相关阅读 更多 +