4.5 Scenario: Receiving a Message from a Queue Asynchronously via Event Callbacks

The following diagram depicts the use of MSMQQueueInfo, MSMQQueue, and MSMQEvent to receive a message from a queue asynchronously using event callbacks. To start with, an MSMQQueueInfo object instance that references a particular queue is required. There are various ways to achieve this; for the purposes of this example, an instance is created and its FormatName property is set. Then the MSMQQueueInfo.Open method is called to open the referenced queue for receive access, which returns an MSMQQueue instance representing the OpenQueueDescriptor. Next, an MSMQEvent object instance is created. The MSMQEvent instance is passed to the MSMQQueue.EnableNotification method. When a message arrives, the server calls the MSMQEvent.Arrived method. In response to that call, the MSMQQueue.Receive method is called, which returns an MSMQMessage instance.

Receiving a message from a queue asynchronously via event callbacks

Figure 7: Receiving a message from a queue asynchronously via event callbacks