前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SetTimer在无窗口和有窗口线程的使用

SetTimer在无窗口和有窗口线程的使用

作者头像
雪影
发布2018-08-02 14:33:17
7700
发布2018-08-02 14:33:17
举报
文章被收录于专栏:流媒体人生流媒体人生

 今天犯了一个粗心的错误,在无窗口线程中,SetTimer中设置计时器ID,而WM_TIMER消息响应函数中得到的计时器ID却不是之前设置的计时器ID.

代码语言:javascript
复制
// 111902.cpp : Defines the entry point for the console application. 
// 
#include "stdafx.h" 
#include "windows.h" 
BOOL DispatchThreadMessage(MSG* pMsg);  
VOID CALLBACK OnTimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime);  
int main(int argc, char* argv[])  
{  
    printf("Hello World!/n");  
    ::SetTimer(NULL,1,1000,OnTimerProc);  
    MSG msg;  
 while (GetMessage(&msg, 0, 0, 0) > 0)  
    {  
 if (msg.hwnd == NULL && DispatchThreadMessage(&msg))  
 continue;  
        TranslateMessage(&msg);  
        DispatchMessage(&msg);  
    }  
 return 0;  
}  
BOOL DispatchThreadMessage(MSG* pMsg)  
{  
 if(pMsg->message == 0x0113)  
    {  
        printf("DispatchThreadMessage: %6d/n",pMsg->wParam);  
 return false;         
    }  
 return false;  
}   
VOID CALLBACK OnTimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)  
{  
    printf("OnTimerProc: %6d/n",idEvent);  
}  

对应计时器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,可由返回时值获取.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2009年11月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档