vc控制台程序中使用定时器

转载自:VC++控制台程序中使用定时器

在新线程中创建定时器,指定回调函数,并且在线程中加入消息处理。

 
  
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 }

 

转载于:https://www.cnblogs.com/lbsx/archive/2010/10/16/1853201.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值