共用方式為


Visual Basic Code Example: Sending Messages Using Multiple-Element Format Names

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

This example provides a private Sub procedure that sends a message to three destination queues based on the computer and queue names provided by the caller.

This procedure creates a direct format name for each queue, and then concatenates those format names to create a single multiple-element format name.

For information on other ways to send messages to multiple destinations, see Multiple-Destination Messaging.

To send a message using multiple-element format names

  1. Declare the objects needed to send the message. This procedure declares the following objects:

  2. Generate a multiple-element format name. This example uses direct format names to reference the three destination queues.

  3. Set the MSMQDestination.FormatName property.

  4. Set message properties. This procedure sets the MSMQMessage.Label property of the message to "Test Message".

  5. Call MSMQMessage.Send to send a copy of the message to each destination queue referenced by the format name.

Note

When using an MSMQDestination object, Message Queuing implicitly opens the destination queues with send access.

Code Example

The following code example requires MSMQ 3.0.

Sub SendMessageMQF( _  
                   strComputerName1 As String, _  
                   strQueueName1 As String, _  
                   strComputerName2 As String, _  
                   strQueueName2 As String, _  
                   strComputerName3 As String, _  
                   strQueueName3 As String _  
                   )  
  
  Dim strFormatName1 As String  
  Dim strFormatName2 As String  
  Dim strFormatName3 As String  
  Dim strMultipleElement As String  
  Dim dest As New MSMQDestination  
  Dim msg As New MSMQMessage  
  
  ' Create multiple-element format name.  
  strFormatName1 = "DIRECT=OS:" & strComputerName1 & "\" & strQueueName1  
  strFormatName2 = "DIRECT=OS:" & strComputerName2 & "\" & strQueueName2  
  strFormatName3 = "DIRECT=OS:" & strComputerName3 & "\" & strQueueName3  
  strMultipleElement = strFormatName1 & "," & strFormatName2 & "," & strFormatName3  
  
  'Set format name of MSMQDestination object.  
  On Error GoTo ErrorHandler  
  dest.FormatName = strMultipleElement  
  MsgBox "Format name is: " + dest.FormatName  
  
  ' Set the message label.  
  msg.Label = "Test Message"  
  
  ' Send the message and close the MSMQDestination object.  
  msg.Send DestinationQueue:=dest  
  dest.Close  
  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
         + Chr(13) + Err.Description  
End Sub