MessageQueue.Receive メソッド ()
MessageQueue で参照されるキューで利用できる最初のメッセージを受信します。この呼び出しは同期呼び出しであるため、メッセージが利用可能になるまで、現在のスレッドの実行をブロックします。
Overloads Public Function Receive() As Message
[C#]
public Message Receive();
[C++]
public: Message* Receive();
[JScript]
public function Receive() : Message;
戻り値
キューで利用できる最初のメッセージを参照する Message 。
例外
例外の種類 | 条件 |
---|---|
MessageQueueException | メッセージ キューの API にアクセスしたときにエラーが発生しました。 |
解説
このオーバーロードを使用して、キューからメッセージを受信したり、キューにメッセージが到達するまで待機したりします。
Receive メソッドを使用すると、メッセージを同期的に読み取ることができるため、キューから削除できます。後続の Receive 呼び出しは、キューにある次のメッセージを返します。これは、新しいメッセージか、または優先順位の高いメッセージです。
キューにある最初のメッセージをキューから削除せずに読み取るには、 Peek メソッドを使用します。 Peek メソッドは、常にキューの最初のメッセージを返します。そのため、より優先順位の高いメッセージがそのキューに到達するまで、後続の呼び出しでも同じメッセージが返されます。
メッセージのキューへの到達を待機している間に、現在のスレッドがブロックされてもいい場合は、 Receive を呼び出します。この Receive メソッドのオーバーロードは無期限のタイムアウトを指定するため、アプリケーションが無限に待機する可能性があります。メッセージを待機せずにアプリケーションの処理を継続する必要がある場合は、非同期メソッド BeginReceive を使用します。
このメソッドが各種のワークグループ モードで使用できるかどうかを次の表に示します。
ワークグループ モード | 使用可否 |
---|---|
ローカル コンピュータ | はい |
ローカル コンピュータ + 直接書式名 | はい |
リモート コンピュータ | いいえ |
リモート コンピュータ + 直接書式名 | はい |
使用例
[Visual Basic, C#, C++] キューからメッセージを受信し、そのメッセージに関する情報を画面に出力する例を次に示します。
Imports System
Imports System.Messaging
Namespace MyProject
' This class represents an object the following example
' sends to a queue and receives from a queue.
Public Class Order
Public orderId As Integer
Public orderTime As DateTime
End Class 'Order
'/ <summary>
'/ Provides a container class for the example.
'/ </summary>
Public Class MyNewQueue
'**************************************************
' Provides an entry point into the application.
'
' This example sends and receives a message from
' a qeue.
'**************************************************
Public Shared Sub Main()
' Create a new instance of the class.
Dim myNewQueue As New MyNewQueue()
' Send a message to a queue.
myNewQueue.SendMessage()
' Receive a message from a queue.
myNewQueue.ReceiveMessage()
Return
End Sub 'Main
'**************************************************
' Sends an Order to a queue.
'**************************************************
Public Sub SendMessage()
' Create a new order and set values.
Dim sentOrder As New Order()
sentOrder.orderId = 3
sentOrder.orderTime = DateTime.Now
' Connect to a queue on the local computer.
Dim myQueue As New MessageQueue(".\myQueue")
' Send the Order to the queue.
myQueue.Send(sentOrder)
Return
End Sub 'SendMessage
'**************************************************
' Receives a message containing an Order.
'**************************************************
Public Sub ReceiveMessage()
' Connect to the a queue on the local computer.
Dim myQueue As New MessageQueue(".\myQueue")
' Set the formatter to indicate the body contains an Order.
myQueue.Formatter = New XmlMessageFormatter(New Type() _
{GetType(MyProject.Order)})
Try
' Receive and format the message.
Dim myMessage As Message = myQueue.Receive()
Dim myOrder As Order = CType(myMessage.Body, Order)
' Display message information.
Console.WriteLine(("Order ID: " + _
myOrder.orderId.ToString()))
Console.WriteLine(("Sent: " + _
myOrder.orderTime.ToString()))
Catch m As MessageQueueException
' Handle Message Queuing exceptions.
Catch e As InvalidOperationException
' Handle invalid serialization format.
Console.WriteLine(e.Message)
' Catch other exceptions as necessary.
End Try
Return
End Sub 'ReceiveMessage
End Class 'MyNewQueue
End Namespace 'MyProject
[C#]
using System;
using System.Messaging;
namespace MyProject
{
// This class represents an object the following example
// sends to a queue and receives from a queue.
public class Order
{
public int orderId;
public DateTime orderTime;
};
/// <summary>
/// Provides a container class for the example.
/// </summary>
public class MyNewQueue
{
//**************************************************
// Provides an entry point into the application.
//
// This example sends and receives a message from
// a queue.
//**************************************************
public static void Main()
{
// Create a new instance of the class.
MyNewQueue myNewQueue = new MyNewQueue();
// Send a message to a queue.
myNewQueue.SendMessage();
// Receive a message from a queue.
myNewQueue.ReceiveMessage();
return;
}
//**************************************************
// Sends an Order to a queue.
//**************************************************
public void SendMessage()
{
// Create a new order and set values.
Order sentOrder = new Order();
sentOrder.orderId = 3;
sentOrder.orderTime = DateTime.Now;
// Connect to a queue on the local computer.
MessageQueue myQueue = new MessageQueue(".\\myQueue");
// Send the Order to the queue.
myQueue.Send(sentOrder);
return;
}
//**************************************************
// Receives a message containing an Order.
//**************************************************
public void ReceiveMessage()
{
// Connect to the a queue on the local computer.
MessageQueue myQueue = new MessageQueue(".\\myQueue");
// Set the formatter to indicate body contains an Order.
myQueue.Formatter = new XmlMessageFormatter(new Type[]
{typeof(MyProject.Order)});
try
{
// Receive and format the message.
Message myMessage = myQueue.Receive();
Order myOrder = (Order)myMessage.Body;
// Display message information.
Console.WriteLine("Order ID: " +
myOrder.orderId.ToString());
Console.WriteLine("Sent: " +
myOrder.orderTime.ToString());
}
catch (MessageQueueException)
{
// Handle Message Queuing exceptions.
}
// Handle invalid serialization format.
catch (InvalidOperationException e)
{
Console.WriteLine(e.Message);
}
// Catch other exceptions as necessary.
return;
}
}
}
[C++]
#using <mscorlib.dll>
#using <system.dll>
#using <system.messaging.dll>
using namespace System;
using namespace System::Messaging;
// This class represents an object the following example
// sends to a queue and receives from a queue.
__gc class Order
{
public:
int orderId;
DateTime orderTime;
};
/// <summary>
/// Provides a container class for the example.
/// </summary>
__gc class MyNewQueue
{
public:
//*************************************************
// Sends an Order to a queue.
//*************************************************
void SendMessage()
{
// Create a new order and set values.
Order* sentOrder = new Order();
sentOrder->orderId = 3;
sentOrder->orderTime = DateTime::Now;
// Connect to a queue on the local computer.
MessageQueue* myQueue = new MessageQueue(S".\\myQueue");
// Send the Order to the queue.
myQueue->Send(sentOrder);
return;
}
//*************************************************
// Receives a message containing an Order.
//*************************************************
void ReceiveMessage()
{
// Connect to the a queue on the local computer.
MessageQueue* myQueue = new MessageQueue(S".\\myQueue");
// Set the formatter to indicate body contains an Order.
Type* p __gc[] = new Type* __gc[1];
p[0] = __typeof(Order);
myQueue->Formatter = new XmlMessageFormatter( p );
try
{
// Receive and format the message.
Message* myMessage = myQueue->Receive();
Order* myOrder = static_cast<Order*>(myMessage->Body);
// Display message information.
Console::WriteLine(S"Order ID: {0}", __box(myOrder->orderId));
Console::WriteLine(S"Sent: {0}", __box(myOrder->orderTime));
}
catch (MessageQueueException*)
{
// Handle Message Queuing exceptions.
}
// Handle invalid serialization format.
catch (InvalidOperationException* e)
{
Console::WriteLine(e->Message);
}
// Catch other exceptions as necessary.
return;
}
};
//*************************************************
// Provides an entry point into the application.
//
// This example sends and receives a message from
// a queue.
//*************************************************
int main()
{
// Create a new instance of the class.
MyNewQueue* myNewQueue = new MyNewQueue();
// Send a message to a queue.
myNewQueue->SendMessage();
// Receive a message from a queue.
myNewQueue->ReceiveMessage();
return 0;
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
.NET Framework セキュリティ:
- 直前の呼び出し元の完全信頼。このメンバは、部分的に信頼されているコードから使用することはできません。詳細の参照先 : 部分信頼コードからのライブラリの使用
参照
MessageQueue クラス | MessageQueue メンバ | System.Messaging 名前空間 | MessageQueue.Receive オーバーロードの一覧 | ReceiveById | ReceiveByCorrelationId | Peek | BeginReceive