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

简介: 今天犯了一个粗心的错误,在无窗口线程中,SetTimer中设置计时器ID,而WM_TIMER消息响应函数中得到的计时器ID却不是之前设置的计时器ID.   [cpp] view plaincopyprint? // 111902.
今天犯了一个粗心的错误,在无窗口线程中,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. }  
// 111902.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include "stdio.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,45,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,可由返回时值获取.
目录
相关文章
|
Java
编写Java程序,车站只剩 50 张从武汉到北京的车票,现有 3 个窗口售卖,用程序模拟售票的过程,使用Runnable解决线程安全问题
编写Java程序,车站只剩 50 张从武汉到北京的车票,现有 3 个窗口售卖,用程序模拟售票的过程,使用Runnable解决线程安全问题
314 0
编写Java程序,车站只剩 50 张从武汉到北京的车票,现有 3 个窗口售卖,用程序模拟售票的过程,使用Runnable解决线程安全问题
|
Windows
【Windows 逆向】OD 调试器工具 ( 显示模块窗口 | 显示记录窗口 | 显示内存窗口 | 显示线程 | 显示句柄 | 显示 CPU | 多窗口界面 )(二)
【Windows 逆向】OD 调试器工具 ( 显示模块窗口 | 显示记录窗口 | 显示内存窗口 | 显示线程 | 显示句柄 | 显示 CPU | 多窗口界面 )(二)
445 0
【Windows 逆向】OD 调试器工具 ( 显示模块窗口 | 显示记录窗口 | 显示内存窗口 | 显示线程 | 显示句柄 | 显示 CPU | 多窗口界面 )(二)
|
Windows
【Windows 逆向】OD 调试器工具 ( 显示模块窗口 | 显示记录窗口 | 显示内存窗口 | 显示线程 | 显示句柄 | 显示 CPU | 多窗口界面 )(一)
【Windows 逆向】OD 调试器工具 ( 显示模块窗口 | 显示记录窗口 | 显示内存窗口 | 显示线程 | 显示句柄 | 显示 CPU | 多窗口界面 )(一)
407 0
【Windows 逆向】OD 调试器工具 ( 显示模块窗口 | 显示记录窗口 | 显示内存窗口 | 显示线程 | 显示句柄 | 显示 CPU | 多窗口界面 )(一)
|
C#
WPF 同一窗口内的多线程/多进程 UI(使用 SetParent 嵌入另一个窗口)
原文 WPF 同一窗口内的多线程/多进程 UI(使用 SetParent 嵌入另一个窗口) WPF 的 UI 逻辑只在同一个线程中,这是学习 WPF 开发中大家几乎都会学习到的经验。如果希望做不同线程的 UI,大家也会想到使用另一个窗口来实现,让每个窗口拥有自己的 UI 线程。
3291 0
|
C#
WPF中窗口控件的跨线程调用
原文:WPF中窗口控件的跨线程调用 在WinForm中,我们要跨线程访问窗口控件,只需要设置属性CheckForIllegalCrossThreadCalls = false;即可。 在WPF中要麻烦一下,同样的不允许跨线程访问,因为没有权限,访问了会抛异常; 没有CheckForIllegalCrossThreadCalls 属性,怎么办? 在WPF中的窗口控件都有一个Dispatcher属性,允许访问控件的线程;既然不允许直接访问,就告诉控件我们要干什么就好了。
2064 0
C#中一道关于线程同步的练习题——模拟多窗口售票
题目:模拟窗口卖票,四个窗口同时对外开放售票,需要按顺序售出。 要求:输出每一张票的售出时间和售出窗口,不能出现票未售出或者被售出多次的情况。 using System; using System.
1757 0