DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
今天犯了一个粗心的错误,在无窗口线程中,SetTimer中设置计时器ID,而WM_TIMER消息响应函数中得到的计时器ID却不是之前设置的计时器ID.

 

  1. // 111902.cpp : Defines the entry point for the console application.   
  2. //   
  3. //#include "stdafx.h"   
  4. #include "stdio.h"   
  5. #include "windows.h"   
  6. BOOL DispatchThreadMessage(MSG* pMsg);  
  7. VOID CALLBACK OnTimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime);  
  8. int main(int argc, char* argv[])  
  9. {  
  10.     printf("Hello World!\n");  
  11.     ::SetTimer(NULL,45,1000,OnTimerProc);  
  12.     MSG msg;  
  13.     while (GetMessage(&msg, 0, 0, 0) > 0)  
  14.     {  
  15.         if (msg.hwnd == NULL && DispatchThreadMessage(&msg))  
  16.             continue;  
  17.         TranslateMessage(&msg);  
  18.         DispatchMessage(&msg);  
  19.     }  
  20.     return 0;  
  21. }  
  22.   
  23. BOOL DispatchThreadMessage(MSG* pMsg)  
  24. {  
  25.     if(pMsg->message == 0x0113)  
  26.     {  
  27.         printf("DispatchThreadMessage: %6d\n",pMsg->wParam);  
  28.         return false;         
  29.     }  
  30.     return false;  
  31. }   
  32. VOID CALLBACK OnTimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)  
  33. {  
  34.     printf("OnTimerProc: %6d\n",idEvent);  
  35. }  

 

 

对应计时器ID的输出的是一个随机数字.

 

原来在msdn中

nIDEvent
[in] Specifies a nonzero timer identifier. If the hWnd parameter is NULL, and the nIDEvent does not match an existing timer then it is ignored and a new timer ID is generated. If the hWnd parameter is not NULL and the window specified by hWnd already has a timer with the value nIDEvent , then the existing timer is replaced by the new timer. When SetTimer replaces a timer, the timer is reset. Therefore, a message will be sent after the current time-out value elapses, but the previously set time-out value is ignored. If the call is not intended to replace an existing timer, nIDEvent should be 0 if the hWnd is NULL.

注:只有当hWnd参数为非空时,计时器的ID为设置的 nIDEvent,  否则系统为你自动生成一个计时器ID,可由返回时值获取.
posted on   DoubleLi  阅读(629)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示