Share via


方法 : .NET Compact Framework で MSMQ を使用します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

メッセージ キュー (MSMQ とも呼ばれます) を使用する .NET Compact Framework アプリケーションの作成は、.NET Framework で使用されるプロセスと似ています。 ただし、Windows CE は .NET Compact Framework で MSMQ に記載されているすべての機能をサポートしません。

メッセージ キューを作成、キューにメッセージを送信およびをすべて同じデバイスに、キューからメッセージを受信コード例を次に示します。 ネットワーク接続は不要です。 単純なクラス、Order は、メッセージ キューのオブジェクトを作成する使用されます。

インストールされてこれらの例をメッセージ キューがされているを前提としています、デバイス上。 メッセージ キュー コンポーネントの取得については、.NET Compact Framework で MSMQ を参照してください。

Order クラスを定義するには

  • プロジェクトに、次のクラスを追加します。

                                ' This class represents an object that
                                ' is sent to and received from the queue.
                                Public
                                Class Order
        Dim ID AsIntegerDim DTime As DateTime
        PublicProperty orderID() AsIntegerGetReturnMe.ID
            EndGetSet(ByVal value AsInteger)
                Me.ID = value
            EndSetEndPropertyPublicProperty orderTime() As DateTime
            GetReturnMe.DTime
            EndGetSet(ByVal value As DateTime)
                Me.DTime = value
            EndSetEndPropertyEndClass
    

メッセージ キューを作成

  • フォームに次のメソッドを追加します。

                                Private
                                Sub CreateQueue()
        ' Determine whether the queue exists.IfNot MessageQueue.Exists(QPath) ThenTry            ' Create the queue if it does not exist.
                myQ = MessageQueue.Create(QPath)
                MessageBox.Show("Queue Created")
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            EndTryElse
            MessageBox.Show("Queue Exists")
        EndIfEndSub
    

キューにメッセージを送信するには

  • フォームに次のメソッドを追加します。

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

キューからメッセージを受信

  • フォームに次のメソッドを追加します。

                                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.
            MessageBox.Show("Order ID: " & _
                myOrder.orderID.ToString() & _
                    Chr(10) & "Sent: " & myOrder.orderTime.ToString())
    
        Catch m As MessageQueueException
            ' Handle Message Queuing exceptions.
            MessageBox.Show(m.Message)
        Catch e As InvalidOperationException
            ' Handle invalid serialization format.
            MessageBox.Show(e.Message)
        EndTryEndSub
    

メッセージ キューをテストするには

  1. ボタンをラベル 送信、 CreateQueueSendMessageToQueue メソッドを呼び出しますフォームに追加します。

  2. ボタンを 受信ReceiveMessageFromQueue メソッドを呼び出すフォームに追加します。

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

参照

概念

.NET Compact Framework で MSMQ

.NET コンパクトなフレームワーク方法を説明したトピックの検索

メッセージ キューとメッセージング テクノロジの背景