Share via


How to: Use MSMQ in the .NET Compact Framework 

Creating .NET Compact Framework applications that use Message Queuing (MSMQ) is similar to the process used in the.NET Framework. However, Windows CE does not support all the features as described in MSMQ in the .NET Compact Framework.

The following code examples show how to create a message queue, send a message to the queue, and receive a message from the queue all on the same device. No network connectivity is required. A simple class, Order, is used to create objects for message queuing.

These examples assume that Message Queuing has been installed on the device. For more information about obtaining the Message Queuing component, see MSMQ in the .NET Compact Framework.

To define the Order class

  • Add the following class to your project.

    ' This class represents an object that
    ' is sent to and received from the queue.
    Public Class Order
        Dim ID As Integer
        Dim DTime As DateTime
        Public Property orderID() As Integer
            Get
                Return Me.ID
            End Get
            Set(ByVal value As Integer)
                Me.ID = value
            End Set
        End Property
        Public Property orderTime() As DateTime
            Get
                Return Me.DTime
            End Get
            Set(ByVal value As DateTime)
                Me.DTime = value
            End Set
        End Property
    End Class
    

To create the message queue

  • Add the following method to your form.

    Private Sub CreateQueue()
        ' Determine whether the queue exists.
        If Not MessageQueue.Exists(QPath) Then
            Try
                ' Create the queue if it does not exist.
                myQ = MessageQueue.Create(QPath)
                MsgBox("Queue Created")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            MsgBox("Queue Exists")
        End If
    End Sub
    

To send a message to the queue

  • Add the following method to your form.

    Private Sub SendMessageToQueue()
        ' Create a new order and set values.
        Dim sendOrder As New Order()
        sendOrder.orderID = 23123
        sendOrder.orderTime = DateTime.Now
        Try
            myQ.Send(sendOrder)
            MsgBox("Message Sent")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    

To receive the message from the queue

  • Add the following method to your form.

    Private Sub ReceiveMessageFromQueue()
        ' Connect to the a queue on the device.
        myQ = New MessageQueue(QPath)
    
        ' Set the formatter to indicate the body contains an Order.
        Dim targetTypes() As Type
        targetTypes = New Type() {GetType(Order)}
        myQ.Formatter = New XmlMessageFormatter(targetTypes)
        Try
            ' Receive and format the message. 
            Dim myMessage As Message = myQ.Receive()
            Dim myOrder As Order = CType(myMessage.Body, Order)
    
            ' Display message information.
            MsgBox("Order ID: " & _
                myOrder.orderID.ToString() & _
                    Chr(10) & "Sent: " & myOrder.orderTime.ToString())
    
        Catch m As MessageQueueException
            ' Handle Message Queuing exceptions.
            MsgBox(m.Message)
        Catch e As InvalidOperationException
            ' Handle invalid serialization format.
            MsgBox(e.Message)
        End Try
    End Sub
    

To test message queuing

  1. Add a button to the form, labeled Send, that calls the CreateQueue and SendMessageToQueue methods.

  2. Add a button to the form, labeled Receive, that calls the ReceiveMessageFromQueue method.

Compiling the Code

This example requires references to the following namespaces:

See Also

Tasks

MSMQ Book Order Application Sample

Concepts

MSMQ in the .NET Compact Framework
.NET Compact Framework How-To Topics
Message Queues and Messaging Technology Backgrounder