次の方法で共有


MessageEnumerator クラス

メッセージ キュー内のメッセージを列挙するための前方向カーソルを提供します。

この型のすべてのメンバの一覧については、MessageEnumerator メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      System.Messaging.MessageEnumerator

Public Class MessageEnumerator
   Inherits MarshalByRefObject
   Implements IEnumerator, IDisposable
[C#]
public class MessageEnumerator : MarshalByRefObject, IEnumerator,
   IDisposable
[C++]
public __gc class MessageEnumerator : public MarshalByRefObject,
   IEnumerator, IDisposable
[JScript]
public class MessageEnumerator extends MarshalByRefObject
   implements IEnumerator, IDisposable

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

キュー内のメッセージとの動的な対話には MessageEnumerator を使います。 MessageQueue クラスを通じて使用できるメソッドは、キュー内のメッセージの動的リストを指す MessageEnumerator か、指定されたメソッドが呼び出された時点でのキューのコピー (スナップショット) を含む配列のいずれかを返すことができます。

静的なスナップショットと違い、列挙子を使うとコレクションを変更できます。 MessageEnumerator を使うと、キューからメッセージを削除でき、その変更はキュー内ですぐに反映されます。

列挙子がキューを問い合わせるときには、列挙子はキューからメッセージを削除しません。列挙子は、現在のカーソル位置のメッセージについての情報を返しますが、キュー内のメッセージはそのままです。

MessageEnumerator はカーソルであり、初期化時に動的リストの先頭に配置されます。リストの順序は、キュー内のメッセージの順序 (メッセージの優先順位に従った順序) と同じです。 MoveNext を呼び出すことで、キュー内の最初のメッセージにカーソルを移動させることができます。列挙子が初期化された後に、 MoveNext を使って残りのメッセージを順に進んでいくことができます。 MoveNext メソッドにタイムアウトを渡すことで、メッセージが使用できるようになるまで待機するかどうかを指定できます。

列挙子は動的であるため、カーソルの現在位置より後に追加されるメッセージ (優先順位が低いためなど) にもアクセスできます。しかし、カーソルの現在位置より前に挿入されているメッセージにはアクセスできません。 MessageEnumerator で後方に移動することはできません。カーソルは前方にだけ移動できます。しかし、 Reset メソッドを使うと、キューの先頭にカーソルを戻すことができます。

キューに対する MessageEnumerator の複数のインスタンスは、それぞれ個別に動作します。同じキューに適用される 2 つの MessageEnumerator のインスタンスを作成できます。その場合、第 1 の MessageEnumerator がキュー内のメッセージに行った変更は、第 2 の列挙子においてすぐに反映されます (第 2 の列挙子が第 1 の列挙子より前に位置しているとき)。しかし、2 つの列挙子の位置が同じときに一方の列挙子がその位置のメッセージを削除した場合は、既に削除されたメッセージの Current プロパティの値を他方の列挙子が取得しようとすると例外がスローされます。

メモ    MessageQueue.DenySharedReceivetrue に設定して MessageQueue のインスタンスを作成すると、アプリケーションがキューに接続している間は他のアプリケーションによって列挙子内のメッセージが変更されなくなります。

使用例

[Visual Basic, C#, C++] キューにあるメッセージの動的リストを取得し、 Priority プロパティが MessagePriority.Lowest に設定されたすべてのメッセージをカウントする例を次に示します。

 
Imports System
Imports System.Messaging

Namespace MyProject

    '/ <summary>
    '/ Provides a container class for the example.
    '/ </summary>
    Public Class MyNewQueue


        '**************************************************
        ' Provides an entry point into the application.
        '         
        ' This example uses a cursor to step through the
        ' messages in a queue and counts the number of 
        ' Lowest priority messages.
        '**************************************************

        Public Shared Sub Main()

            ' Create a new instance of the class.
            Dim myNewQueue As New MyNewQueue()

            ' Output the count of Lowest priority messages.
            myNewQueue.CountLowestPriority()

            Return

        End Sub 'Main


        '**************************************************
        ' Iterates through messages in a queue and examines
        ' their priority.
        '**************************************************

        Public Sub CountLowestPriority()

            ' Holds the count of Lowest priority messages.
            Dim numberItems As Int32 = 0

            ' Connect to a queue.
            Dim myQueue As New MessageQueue(".\myQueue")

            ' Get a cursor into the messages in the queue.
            Dim myEnumerator As MessageEnumerator = _
                myQueue.GetMessageEnumerator()

            ' Specify that the messages's priority should be read.
            myQueue.MessageReadPropertyFilter.Priority = True

            ' Move to the next message and examine its priority.
            While myEnumerator.MoveNext()

                ' Increase the count if the priority is Lowest.
                If myEnumerator.Current.Priority = _
                    MessagePriority.Lowest Then
                    numberItems += 1
                End If

            End While

            ' Display final count.
            Console.WriteLine(("Lowest priority messages: " + _
                numberItems.ToString()))

            Return

        End Sub 'CountLowestPriority

    End Class 'MyNewQueue
End Namespace 'MyProject

[C#] 
using System;
using System.Messaging;

namespace MyProject
{
    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {

        //**************************************************
        // Provides an entry point into the application.
        //         
        // This example uses a cursor to step through the
        // messages in a queue and counts the number of 
        // Lowest priority messages.
        //**************************************************

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            // Output the count of Lowest priority messages.
            myNewQueue.CountLowestPriority();
                        
            return;
        }


        //**************************************************
        // Iterates through messages in a queue and examines
        // their priority.
        //**************************************************
        
        public void CountLowestPriority()
        {
            // Holds the count of Lowest priority messages.
            uint numberItems = 0;

            // Connect to a queue.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");
    
            // Get a cursor into the messages in the queue.
            MessageEnumerator myEnumerator = 
                myQueue.GetMessageEnumerator();

            // Specify that the messages's priority should be read.
            myQueue.MessageReadPropertyFilter.Priority = true;

            // Move to the next message and examine its priority.
            while(myEnumerator.MoveNext())
            {
                // Increase the count if priority is Lowest.
                if(myEnumerator.Current.Priority == 
                    MessagePriority.Lowest)
                    
                    numberItems++;
            }

            // Display final count.
            Console.WriteLine("Lowest priority messages: " + 
                numberItems.ToString());
            
            return;
        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;

__gc class MyNewQueue 
{
public:
    void CountLowestPriority() 
    {
        // Holds the count of Lowest priority messages.
        UInt32 numberItems = 0;

        // Connect to a queue.
        MessageQueue* myQueue = new MessageQueue(S".\\myQueue");

        // Get a cursor into the messages in the queue.
        MessageEnumerator* myEnumerator = 
            myQueue->GetMessageEnumerator();

        // Specify that the messages's priority should be read.
        myQueue->MessageReadPropertyFilter->Priority = true;

        // Move to the next message and examine its priority.
        while(myEnumerator->MoveNext())
        {
            // Increase the count if priority is Lowest.
            if (myEnumerator->Current->Priority == 
                MessagePriority::Lowest)

                numberItems++;
        }

        // Display final count.
        Console::WriteLine(S"Lowest priority messages: {0}", __box(numberItems));

        return;
    }
};

int main() 
{
    // Create a new instance of the class.
    MyNewQueue* myNewQueue = new MyNewQueue();

    // Output the count of Lowest priority messages.
    myNewQueue->CountLowestPriority();

    return 0;
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Messaging

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Messaging (System.Messaging.dll 内)

参照

MessageEnumerator メンバ | System.Messaging 名前空間 | Message | MessageQueue.GetMessageEnumerator