Export (0) Print
Expand All

Asynchronous Reading

When messages are read synchronously, the reading function or method returns only when the I/O operation is completed. When messages are read asynchronously, the reading function or method returns immediately, but with notification that an asynchronous I/O operation is pending. This means that the time between initiation of the asynchronous read operation and its completion can be used to do something useful, which may be to initiate another read operation. There are four mechanisms that can be used to read messages asynchronously:

  • Callback functions

  • Windows events

  • Completion ports (Not available for dependent clients.)

  • Message Queuing COM components

When using a callback function, Message Queuing reads the message by calling the callback function that is currently registered. The function is called if a message is immediately available, when a message arrives, or when the time for reading a message expires. Only 63 callbacks can be registered at the same time.

When using an event mechanism, an Overlapped structure provides a valid handle (hEvent field) to an event object. When a suitable message arrives, or a time-out occurs, the event object is set to the signaled state.

When using a completion port, a queue handle can be associated with the port to receive messages asynchronously. For more information, see CreateIOCompletionPort.

When using the Message Queuing COM components, define an event handler that is notified when a message arrives or a time-out occurs. The user-defined event handler itself is invoked from an internally implemented callback function (limited to 64 callbacks, one of which is used by the runtime). Therefore, all callback features and restrictions apply when using the COM components.

COM components invoke an ArrivedError event handler with MQ_ERROR_IO_TIMEOUT when a time-out occurs.

Cursors behave the same way in synchronous and asynchronous operations. However, when using a cursor to read messages asynchronously do not use the same cursor in overlapping read operations. For example, when moving a cursor you should not fire a second receive operation before the previous receive is completed. This type of action may lead to unexpected behavior.

When reading messages in a queue, Message Queuing can peek at the messages (leaving them in the queue) or retrieve them (removing the messages from the queue).

More Information

© 2015 Microsoft