VB中使用MSMQ

VB中使用MSMQ

Option Explicit

'接收消息的队列,要求模块级变量
Dim m_RevQueue As MSMQQueue

'接收事件接口
Dim WithEvents m_RevEvent As MSMQEvent

Private Sub Form_Load()

    '创建消息队列
    Dim QueueInfo As New MSMQQueueInfo
    QueueInfo.PathName = ".\Private$\MyQueue"
    QueueInfo.Label = "MyQueue"
    'QueueInfo.Delete
    QueueInfo.Create
   
    '设置接收事件接口
    Set m_RevEvent = New MSMQEvent
    Set m_RevQueue = QueueInfo.Open(MQACCESS.MQ_RECEIVE_ACCESS, MQSHARE.MQ_DENY_NONE) '接收权限
    m_RevQueue.EnableNotification m_RevEvent

End Sub

Private Sub cmdSend_Click()
   
    Dim QueueInfo As New MSMQQueueInfo
    QueueInfo.PathName = ".\Private$\MyQueue"
    QueueInfo.Label = "MyQueue"
   
    '打开队列,发送消息
    Dim MsgQueue As MSMQQueue
    Set MsgQueue = QueueInfo.Open(MQACCESS.MQ_SEND_ACCESS, MQSHARE.MQ_DENY_NONE) '发送权限
   
    If (MsgQueue.IsOpen()) Then
        Dim Msg As New MSMQMessage
        Msg.Label = "MSMQ消息"
        Msg.Body = "可以是任何变量,只要支持Idispatch接口和持续化接口!"
        Msg.Send MsgQueue
        MsgQueue.Close
     End If

End Sub


Private Sub m_RevEvent_Arrived(ByVal Queue As Object, ByVal Cursor As Long)
  '消息到来
  Dim theQueue As MSMQQueue
  Set theQueue = Queue
 
  Dim Msg As MSMQMessage
  Set Msg = theQueue.Receive() '得到消息
 
  theQueue.EnableNotification m_RevEvent '接收下一个消息
 
  MsgBox Msg.Body, vbInformation, Msg.Label
 
End Sub

你可能感兴趣的:(VB中使用MSMQ)