MessageQueue.BeginReceive メソッド
メッセージの受信を開始し、完了したときにイベント ハンドラに通知するようにメッセージ キューに指示して、非同期の受信操作を実行します。
オーバーロードの一覧
タイムアウトのない非同期の受信操作を実行します。操作は、キューのメッセージが利用可能になるまで完了しません。
[Visual Basic] Overloads Public Function BeginReceive() As IAsyncResult
指定したタイムアウトのある非同期の受信操作を実行します。この操作は、メッセージをキューで使用できるようになる、タイムアウトが発生するまで完了しません。
[Visual Basic] Overloads Public Function BeginReceive(TimeSpan) As IAsyncResult
[JScript] public function BeginReceive(TimeSpan) : IAsyncResult;
指定したタイムアウトと指定した状態オブジェクトを持つ非同期の受信操作を実行します。状態オブジェクトは、操作の有効期間を通じて関連付けられた情報を提供します。この操作は、メッセージをキューで使用できるようになる、タイムアウトが発生するまで完了しません。
[Visual Basic] Overloads Public Function BeginReceive(TimeSpan, Object) As IAsyncResult
[C++] public: IAsyncResult* BeginReceive(TimeSpan, Object*);
[JScript] public function BeginReceive(TimeSpan, Object) : IAsyncResult;
指定したタイムアウトと指定した状態オブジェクトを持つ非同期の受信操作を実行します。状態オブジェクトは、操作の有効期間を通じて関連付けられた情報を提供します。このオーバーロードは、コールバックを通じて操作のイベント ハンドラ ID の通知を受信します。この操作は、メッセージをキューで使用できるようになる、タイムアウトが発生するまで完了しません。
[Visual Basic] Overloads Public Function BeginReceive(TimeSpan, Object, AsyncCallback) As IAsyncResult
[C#] public IAsyncResult BeginReceive(TimeSpan, object, AsyncCallback);
[C++] public: IAsyncResult* BeginReceive(TimeSpan, Object*, AsyncCallback*);
[JScript] public function BeginReceive(TimeSpan, Object, AsyncCallback) : IAsyncResult;
使用例
[Visual Basic, C#, C++] 非同期要求をチェインする例を次に示します。ローカル コンピュータに "myQueue" というキューがあることを前提にしています。 Main
関数は、 MyReceiveCompleted
ルーチンで処理される非同期操作を開始します。 MyReceiveCompleted
は現在のメッセージを処理し、新しい非同期の受信操作を開始します。
[Visual Basic, C#, C++] メモ ここでは、BeginReceive のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
Imports System
Imports System.Messaging
Imports System.Threading
Namespace MyProject
'/ <summary>
'/ Provides a container class for the example.
'/ </summary>
Public Class MyNewQueue
' Define static class members.
Private Shared signal As New ManualResetEvent(False)
Private Shared count As Integer = 0
'**************************************************
' Provides an entry point into the application.
'
' This example performs asynchronous receive
' operation processing.
'**************************************************
Public Shared Sub Main()
' Create an instance of MessageQueue. Set its formatter.
Dim myQueue As New MessageQueue(".\myQueue")
myQueue.Formatter = New XmlMessageFormatter(New Type() _
{GetType([String])})
' Add an event handler for the ReceiveCompleted event.
AddHandler myQueue.ReceiveCompleted, AddressOf _
MyReceiveCompleted
' Begin the asynchronous receive operation.
myQueue.BeginReceive()
signal.WaitOne()
' Do other work on the current thread.
Return
End Sub 'Main
'***************************************************
' Provides an event handler for the ReceiveCompleted
' event.
'***************************************************
Private Shared Sub MyReceiveCompleted(ByVal [source] As _
[Object], ByVal asyncResult As ReceiveCompletedEventArgs)
Try
' Connect to the queue.
Dim mq As MessageQueue = CType([source], MessageQueue)
' End the asynchronous receive operation.
Dim m As Message = _
mq.EndReceive(asyncResult.AsyncResult)
count += 1
If count = 10 Then
signal.Set()
End If
' Restart the asynchronous receive operation.
mq.BeginReceive()
Catch
' Handle sources of MessageQueueException.
' Handle other exceptions.
End Try
Return
End Sub 'MyReceiveCompleted
End Class 'MyNewQueue
End Namespace 'MyProject
[C#]
using System;
using System.Messaging;
using System.Threading;
namespace MyProject
{
/// <summary>
/// Provides a container class for the example.
/// </summary>
public class MyNewQueue
{
// Define static class members.
static ManualResetEvent signal = new ManualResetEvent(false);
static int count = 0;
//**************************************************
// Provides an entry point into the application.
//
// This example performs asynchronous receive
// operation processing.
//**************************************************
public static void Main()
{
// Create an instance of MessageQueue. Set its formatter.
MessageQueue myQueue = new MessageQueue(".\\myQueue");
myQueue.Formatter = new XmlMessageFormatter(new Type[]
{typeof(String)});
// Add an event handler for the ReceiveCompleted event.
myQueue.ReceiveCompleted +=
new ReceiveCompletedEventHandler(MyReceiveCompleted);
// Begin the asynchronous receive operation.
myQueue.BeginReceive();
signal.WaitOne();
// Do other work on the current thread.
return;
}
//***************************************************
// Provides an event handler for the ReceiveCompleted
// event.
//***************************************************
private static void MyReceiveCompleted(Object source,
ReceiveCompletedEventArgs asyncResult)
{
try
{
// Connect to the queue.
MessageQueue mq = (MessageQueue)source;
// End the asynchronous receive operation.
Message m = mq.EndReceive(asyncResult.AsyncResult);
count += 1;
if (count == 10)
{
signal.Set();
}
// Restart the asynchronous receive operation.
mq.BeginReceive();
}
catch(MessageQueueException)
{
// Handle sources of MessageQueueException.
}
// Handle other exceptions.
return;
}
}
}
[C++]
#using <mscorlib.dll>
#using <system.dll>
#using <system.messaging.dll>
using namespace System;
using namespace System::Messaging;
using namespace System::Threading;
__gc class MyNewQueue
{
public:
// Define static class members.
static ManualResetEvent* signal = new ManualResetEvent(false);
static int count = 0;
// Provides an event handler for the ReceiveCompleted
// event.
public:
static void MyReceiveCompleted(Object* source,
ReceiveCompletedEventArgs* asyncResult)
{
try
{
// Connect to the queue.
MessageQueue* mq = dynamic_cast<MessageQueue*>(source);
// End the asynchronous receive operation.
mq->EndReceive(asyncResult->AsyncResult);
count += 1;
if (count == 10)
{
signal->Set();
}
// Restart the asynchronous receive operation.
mq->BeginReceive();
}
catch (MessageQueueException*)
{
// Handle sources of MessageQueueException.
}
// Handle other exceptions.
return;
}
};
// Provides an entry point into the application.
//
// This example performs asynchronous receive
// operation processing.
int main()
{
// Create an instance of MessageQueue. Set its formatter.
MessageQueue* myQueue = new MessageQueue(S".\\myQueue");
Type* p __gc[] = new Type* __gc[1];
p[0] = __typeof(String);
myQueue->Formatter = new XmlMessageFormatter( p );
// Add an event handler for the ReceiveCompleted event.
myQueue->ReceiveCompleted += new ReceiveCompletedEventHandler(0, MyNewQueue::MyReceiveCompleted);
// Begin the asynchronous receive operation.
myQueue->BeginReceive();
MyNewQueue::signal->WaitOne();
// Do other work on the current thread.
return 0;
}
[Visual Basic, C#, C++] 非同期要求をキューに入れる例を次に示します。 BeginReceive 呼び出しは、戻り値に AsyncWaitHandle を使用します。 Main
ルーチンは、すべての非同期操作が完了するまで待機してから終了します。
Imports System
Imports System.Messaging
Imports System.Threading
Namespace MyProject
'/ <summary>
'/ Provides a container class for the example.
'/ </summary>
Public Class MyNewQueue
'**************************************************
' Provides an entry point into the application.
'
' This example performs asynchronous receive
' operation processing.
'**************************************************
Public Shared Sub Main()
' Create an instance of MessageQueue. Set its formatter.
Dim myQueue As New MessageQueue(".\myQueue")
myQueue.Formatter = New XmlMessageFormatter(New Type() _
{GetType([String])})
' Add an event handler for the ReceiveCompleted event.
AddHandler myQueue.ReceiveCompleted, AddressOf _
MyReceiveCompleted
' Define wait handles for multiple operations.
Dim waitHandleArray(10) As WaitHandle
Dim i As Integer
For i = 0 To 9
' Begin asynchronous operations.
waitHandleArray(i) = _
myQueue.BeginReceive().AsyncWaitHandle
Next i
' Specify to wait for all operations to return.
WaitHandle.WaitAll(waitHandleArray)
Return
End Sub 'Main
'***************************************************
' Provides an event handler for the ReceiveCompleted
' event.
'***************************************************
Private Shared Sub MyReceiveCompleted(ByVal [source] As _
[Object], ByVal asyncResult As ReceiveCompletedEventArgs)
Try
' Connect to the queue.
Dim mq As MessageQueue = CType([source], MessageQueue)
' End the asynchronous receive operation.
Dim m As Message = _
mq.EndReceive(asyncResult.AsyncResult)
' Process the message here.
Console.WriteLine("Message received.")
Catch
' Handle sources of MessageQueueException.
' Handle other exceptions.
End Try
Return
End Sub 'MyReceiveCompleted
End Class 'MyNewQueue
End Namespace 'MyProject
[C#]
using System;
using System.Messaging;
using System.Threading;
namespace MyProject
{
/// <summary>
/// Provides a container class for the example.
/// </summary>
public class MyNewQueue
{
//**************************************************
// Provides an entry point into the application.
//
// This example performs asynchronous receive
// operation processing.
//**************************************************
public static void Main()
{
// Create an instance of MessageQueue. Set its formatter.
MessageQueue myQueue = new MessageQueue(".\\myQueue");
myQueue.Formatter = new XmlMessageFormatter(new Type[]
{typeof(String)});
// Add an event handler for the ReceiveCompleted event.
myQueue.ReceiveCompleted +=
new ReceiveCompletedEventHandler(MyReceiveCompleted);
// Define wait handles for multiple operations.
WaitHandle[] waitHandleArray = new WaitHandle[10];
for(int i=0; i<10; i++)
{
// Begin asynchronous operations.
waitHandleArray[i] =
myQueue.BeginReceive().AsyncWaitHandle;
}
// Specify to wait for all operations to return.
WaitHandle.WaitAll(waitHandleArray);
return;
}
//***************************************************
// Provides an event handler for the ReceiveCompleted
// event.
//***************************************************
private static void MyReceiveCompleted(Object source,
ReceiveCompletedEventArgs asyncResult)
{
try
{
// Connect to the queue.
MessageQueue mq = (MessageQueue)source;
// End the asynchronous receive operation.
Message m = mq.EndReceive(asyncResult.AsyncResult);
// Process the message here.
Console.WriteLine("Message received.");
}
catch(MessageQueueException)
{
// Handle sources of MessageQueueException.
}
// Handle other exceptions.
return;
}
}
}
[C++]
#using <mscorlib.dll>
#using <system.dll>
#using <system.messaging.dll>
using namespace System;
using namespace System::Messaging;
using namespace System::Threading;
__gc class MyNewQueue
{
// Provides an event handler for the ReceiveCompleted
// event.
public:
static void MyReceiveCompleted(Object* source,
ReceiveCompletedEventArgs* asyncResult)
{
try
{
// Connect to the queue.
MessageQueue* mq = dynamic_cast<MessageQueue*>(source);
// End the asynchronous receive operation.
mq->EndReceive(asyncResult->AsyncResult);
// Process the message here.
Console::WriteLine(S"Message received.");
}
catch (MessageQueueException*)
{
// Handle sources of MessageQueueException.
}
// Handle other exceptions.
return;
}
};
// Provides an entry point into the application.
//
// This example performs asynchronous receive
// operation processing.
int main()
{
// Create an instance of MessageQueue. Set its formatter.
MessageQueue* myQueue = new MessageQueue(S".\\myQueue");
Type* p __gc[] = new Type* __gc[1];
p[0] = __typeof(String);
myQueue->Formatter = new XmlMessageFormatter( p );
// Add an event handler for the ReceiveCompleted event.
myQueue->ReceiveCompleted += new ReceiveCompletedEventHandler(0, MyNewQueue::MyReceiveCompleted);
// Define wait handles for multiple operations.
WaitHandle* waitHandleArray[] = new WaitHandle*[10];
for (int i=0; i<10; i++)
{
// Begin asynchronous operations.
waitHandleArray->Item[i] = myQueue->BeginReceive()->AsyncWaitHandle;
}
// Specify to wait for all operations to return.
WaitHandle::WaitAll(waitHandleArray);
return 0;
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。