Reading Messages Asynchronously (COM) (Windows Embedded CE 6.0)
1/6/2010
The MSMQEvent object used to read messages asynchronously, is implemented in terms of callbacks, and is limited to 64 callbacks per process. Internally, Message Queuing uses the WaitForMultipleObjects API which is limited to 64 objects per process.
When reading messages asynchronously, MSMQQueue.EnableNotification must be called for each message read from the queue.
Typically, EnableNotification is called for the first message to be read and then again in the arrived event handler to read another message. Notification returns a single Arrived event for each message found in the queue.
The following illustration shows the basic programming model used for reading messages asynchronously. It includes two calls to MSMQQueue.EnableNotification, plus a call to read the message from the queue.
The model in the diagram shows that there are many ways to combine the two calls to MSMQQueue.EnableNotification and the call to read the message in the queue.
For example, to purge all the messages in a queue, you could call MSMQQueue.EnableNotification with its Cursor parameter set to MQMSG_CURRENT, call MSMQQueue.ReceiveCurrent to retrieve the message from the queue, and call MSMQQueue.EnableNotification with Cursor set to MQMSG_CURRENT.
However, not all combinations necessarily make good programming sense. For example, it is possible to write an event handler that would only read every other message in the queue.
The initial call to MSMQQueue.EnableNotification and the subsequent call from the event handler can tell Message Queuing to check if a message is in the queue at all (Cursor = MQMSG_FIRST), if a message is at the current cursor location (Cursor = MQMSG_CURRENT), or if a message is at the next position after the cursor (Cursor = MQMSG_NEXT).
The default is to trigger the Arrived event when Message Queuing finds any message in the queue (Cursor = MQMSG_FIRST).
The calls to read the message in the queue include: MSMQQueue.Receive, MSMQQueue.ReceiveCurrent, MSMQQueue.Peek, MSMQQueue.PeekCurrent, or MSMQQueue.PeekNext.
Note
Each call to MSMQQueue.EnableNotification, plus the calls to MSMQQueue.ReceiveCurrent, MSMQQueue.PeekCurrent, or MSMQQueue.PeekNext, provides numerous ways to navigate through the queue. Each call can affect how the cursor is moved through the queue.
To find a specific message asynchronously
Obtain a valid MSMQQueueInfo object.
Call MSMQQueueInfo.Open to open queue with receive access and call MSMQQueue.EnableNotification to start notification.
Write Arrived event handler.
Write Arrived Error event handler.
See Also
Concepts
Reading Messages in a Queue (COM)
MSMQ COM Support
Using the COM Components
MSMQ Security