消息路由--发布日期:2008-05-12 14:31

原创 2008年10月02日 19:44:00

在MSDN上看到的,对理解消息队列很有用。

消息有两种:队列消息和非队列消息。

队列消息的传递方式:系统维护了一个单独的系统消息队列,并为每一个GUI线程维护一个消息队列,当队列消息,比如鼠标,键盘事件引发的消息,将先被放到系统消息队列中,然后按照队列的操作规则,系统一次移除一个消息,并检查该消息,然后发送到产生该目标窗口的线程消息队列中,线程从自己的消息队列中移除消息然后让系统将其发送到窗口过程。

非队列消息:不经过系统消息队列和线程消息队列的迂回,直接发送到目标窗口过程。

在队列消息中WM_PAINT message, the WM_TIMER message, and the WM_QUIT message三种消息是被放在消息队列的队尾的,以确保其他消息的FIFO特性。线程通过GetMessage 从线程消息队列中移除消息, 通过<?XML:NAMESPACE PREFIX = MSHELP />DispatchMessage 驱使系统将消息发送到相应的窗口过程。

Message RoutingThe thread removes messages from its queue and directs the system to send them to the appropriate window procedure for processing.

The system uses two methods to route messages to a window procedure: posting messages to a first-in, first-out queue called a message queue, a system-defined memory object that temporarily stores messages, and sending messages directly to a window procedure.

Messages posted to a message queue are called queued messages. They are primarily the result of user input entered through the mouse or keyboard, such as WM_MOUSEMOVE, WM_LBUTTONDOWN, WM_KEYDOWN, and WM_CHAR messages. Other queued messages include the timer, paint, and quit messages: WM_TIMER, WM_PAINT, and WM_QUIT. Most other messages, which are sent directly to a window procedure, are called nonqueued messages.

Queued Messages

The system can display any number of windows at a time. To route mouse and keyboard input to the appropriate window, the system uses message queues.

The system maintains a single system message queue and one thread-specific message queue for each graphical user interface (GUI) thread. To avoid the overhead of creating a message queue for non–GUI threads, all threads are created initially without a message queue. The system creates a thread-specific message queue only when the thread makes its first call to one of the User or Windows Graphics Device Interface (GDI) functions.

Whenever the user moves the mouse, clicks the mouse buttons, or types on the keyboard, the device driver for the mouse or keyboard converts the input into messages and places them in the system message queue. The system removes the messages, one at a time, from the system message queue, examines them to determine the destination window, and then posts them to the message queue of the thread that created the destination window. A thread's message queue receives all mouse and keyboard messages for the windows created by the thread. The thread removes messages from its queue and directs the system to send them to the appropriate window procedure for processing.

With the exception of the WM_PAINT message, the WM_TIMER message, and the WM_QUIT message, the system always posts messages at the end of a message queue. This ensures that a window receives its input messages in the proper first in, first out (FIFO) sequence. The WM_PAINT message, the WM_TIMER message, and the WM_QUIT message, however, are kept in the queue and are forwarded to the window procedure only when the queue contains no other messages. In addition, multiple WM_PAINT messages for the same window are combined into a single WM_PAINT message, consolidating all invalid parts of the client area into a single area. Combining WM_PAINT messages reduces the number of times a window must redraw the contents of its client area.

The system posts a message to a thread's message queue by filling an MSG structure and then copying it to the message queue. Information in MSG includes: the handle of the window for which the message is intended, the message identifier, the two message parameters, the time the message was posted, and the mouse cursor position. A thread can post a message to its own message queue or to the queue of another thread by using the PostMessage or PostThreadMessage function.

An application can remove a message from its queue by using the GetMessage function. To examine a message without removing it from its queue, an application can use the PeekMessage function. This function fills MSG with information about the message.

After removing a message from its queue, an application can use the DispatchMessage function to direct the system to send the message to a window procedure for processing. DispatchMessage takes a pointer to MSG that was filled by a previous call to the GetMessage or PeekMessage function. DispatchMessage passes the window handle, the message identifier, and the two message parameters to the window procedure, but it does not pass the time the message was posted or mouse cursor position. An application can retrieve this information by calling the GetMessageTime and GetMessagePos functions while processing a message.

A thread can use the WaitMessage function to yield control to other threads when it has no messages in its message queue. The function suspends the thread and does not return until a new message is placed in the thread's message queue.

You can call the SetMessageExtraInfo function to associate a value with the current thread's message queue. Then call the GetMessageExtraInfo function to get the value associated with the last message retrieved by the GetMessage or PeekMessage function.

Nonqueued Messages

Nonqueued messages are sent immediately to the destination window procedure, bypassing the system message queue and thread message queue. The system typically sends nonqueued messages to notify a window of events that affect it. For example, when the user activates a new application window, the system sends the window a series of messages, including WM_ACTIVATE, WM_SETFOCUS, and WM_SETCURSOR. These messages notify the window that it has been activated, that keyboard input is being directed to the window, and that the mouse cursor has been moved within the borders of the window. Nonqueued messages can also result when an application calls certain system functions. For example, the system sends the WM_WINDOWPOSCHANGED message after an application uses the SetWindowPos function to move a window.

Some functions that send nonqueued messages are BroadcastSystemMessage, BroadcastSystemMessageEx, SendMessage, SendMessageTimeout, and SendNotifyMessage.

SQL Server日期格式处理-把14位字符串返回成日期格式

 /**//*描述:14位字符串转化成日期格式输出DEMO:select dbo.fn_get_datetime_from_char14(20080304110102)*/create functio...
  • whbo
  • whbo
  • 2008年04月25日 15:07
  • 1015

poi读取Excel时日期为数字 的解决方法 转载 2017年12月14日 10:31:14 标签:poi /excel /java 19 在使用poi解析Excel文件时,会发现,表格中的日期解析出

poi读取Excel时日期为数字 的解决方法 转载 2017年12月14日 10:31:14 标签:poi /excel /java 19 在使用p...
  • ycmnzmm
  • ycmnzmm
  • 2017年12月17日 21:27
  • 74

RabbitMQ学习之路由(java)

参考:http://blog.csdn.net/lmj623565791/article/details/37669573 使用direct类型实现:消息会被推送至绑定键(binding key)和...
  • tianwei7518
  • tianwei7518
  • 2014年11月07日 08:52
  • 3062

RabbitMQ知识盘点【壹】_消息队列介绍及三种消息路由模式

最近在看消息队列的书籍,把一些收获总结一下。 首先说说什么是消息队列。这里就不说那种教科书的定义了,以我的理解,消息队列就是通过接收和发送消息,使不同的应用系统连接起来。实现了业务系统的解耦...
  • xinzun
  • xinzun
  • 2018年01月08日 14:01
  • 61

[SOA] Mule ESB 3.x 入门(三)—— 消息路由

在前面两篇博文基础上,继续来看看如何利用 Mule 来实现简单的消息路由。消息路由是ESB平台上最基本最重要的功能,它是指通过一定的规则将消息从一个端点发送到另一端点。通常ESB平台上会统一管理很多服...
  • fangxinggood
  • fangxinggood
  • 2013年10月23日 01:41
  • 4372

RabbitMQ学习总结(6)——消息的路由分发机制详解

一、Routing(路由) (using the Java client) 在前面的学习中,构建了一个简单的日志记录系统,能够广播所有的日志给多个接收者,在该部分学习中,将添加一个新的特点,就是可以只...
  • u012562943
  • u012562943
  • 2015年12月23日 18:07
  • 2115

JS判断结束时间不能小于开始时间并且间隔不能超过31天

起始时间: 终止时间: var first = document.getElementById("first").value...
  • kiss_vicente
  • kiss_vicente
  • 2012年08月17日 15:55
  • 9925

MFC命令消息路由过程(视图、框架、应用)

一、环境 IDE:VC6.0 OS:WindowsXp 二、编写测试代码和环境配置 新建一个Win32 Application 选择 “A Simple Win32 Application” 打开st...
  • eldn__
  • eldn__
  • 2014年07月26日 17:31
  • 2180

分布式异步消息框架构建笔记4——分布式消息路由

上一篇实现了消息的自动路由,这边写了一个小测试,大家可以猜一下运行输出结果是什么? public class RouterTest { public static void...
  • IceSun963
  • IceSun963
  • 2014年12月10日 12:01
  • 1232

WSO2——(8)ESB功能:消息路由

这篇文章介绍如何通过WSO2 ESB实现消息路由的功能:基于内容的路由,根据消息内容的不同路由到不同的服务进行处理。 场景:投诉管理 描述:公众发起对律师或公证员的投诉,输入0转到律师服务进行处理,输...
  • szh1124
  • szh1124
  • 2015年02月15日 13:14
  • 1833
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:消息路由--发布日期:2008-05-12 14:31
举报原因:
原因补充:

(最多只允许输入30个字)