Visual Basic Code Example: Reading Messages Synchronously

The following example provides a Sub procedure that synchronously reads each message in a known queue, removing each message as it is read.

For information about reading messages synchronously, see Synchronous Reading.

To read messages synchronously

  1. Declare the objects needed to retrieve the message.

ms705974.note(en-us,VS.85).gifNote
When declaring the MSMQMessage object for reading messages, the New keyword cannot be used. If it were used, the object reference would never be empty, and it would be impossible for the application to determine when no message is available.

  1. Obtain an MSMQQueueInfo object. The following example initializes the MSMQQueueInfo object by setting the MSMQQueueInfo.PathName property using the computer name and queue name provided by the caller.

    Because this procedure sets the MSMQQueueInfo.PathName property of the MSMQQueueInfo object, Message Queuing must obtain the format name of the queue before opening the queue. The format name of a public queue must be retrieved from the directory service, and the format name of a local private queue can be obtained from information stored on the local computer. However, a remote private queue cannot be opened unless the MSMQQueueInfo.FormatName property is set with a direct format name. This procedure can be modified to receive the format name from the caller or to generate a direct format name. The applicable format name can then be used to set the FormatName property. For more information, see Format Names.

  2. Call MSMQQueueInfo.Open to open the queue with receive access. When opening a queue with receive access the application can peek at or retrieve the messages in the queue.

  3. Using a loop, call MSMQQueue.Receive to read each messages in the queue.

  4. When there are no messages left, call MSMQQueue.Close to release resources used to open the queue and exit the Sub procedure.

Code Example

The following code example can be run on all versions of Message Queuing.

Sub ReadingDestQueue( _
                     strComputerName As String, _
                     strQueueName As String _
                     )
  Dim qinfo As New MSMQQueueInfo
  Dim q As MSMQQueue
  Dim msg As MSMQMessage

  ' Associate the MSMQQueueInfo object with the queue.
  qinfo.PathName = strComputerName & "\" & strQueueName

  ' Open the queue.
  On Error GoTo ErrorHandler
  Set q = qinfo.Open(Access:=MQ_RECEIVE_ACCESS, _
                         ShareMode:=MQ_DENY_NONE)
                     
  ' Read messages in the queue.
  Do While True
    Set msg = q.Receive(ReceiveTimeout:=1000)
    If msg Is Nothing Then
      MsgBox "No messages. Closing queue."
      q.Close
      Exit Sub
    End If
    MsgBox "A message was removed from the queue."
  Loop

ErrorHandler:
  MsgBox "Error " + Hex(Err.Number) + " was returned." _
         + Chr(13) + Err.Description
  If Not q Is Nothing And q.IsOpen2 Then
    q.Close
  EndIf
End Sub
Show:
© 2014 Microsoft