MessageEnumeratorとは? わかりやすく解説

MessageEnumerator クラス

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

名前空間: System.Messaging
アセンブリ: System.Messaging (system.messaging.dll 内)
構文構文

Public Class MessageEnumerator
    Inherits MarshalByRefObject
    Implements IEnumerator, IDisposable
Dim instance As MessageEnumerator
public class MessageEnumerator : MarshalByRefObject,
 IEnumerator, IDisposable
public ref class MessageEnumerator : public
 MarshalByRefObject, IEnumerator, IDisposable
public class MessageEnumerator extends MarshalByRefObject
 implements IEnumerator, IDisposable
public class MessageEnumerator extends
 MarshalByRefObject implements IEnumerator, IDisposable
解説解説

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

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

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

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

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

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

メモメモ

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

使用例使用例

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

Imports System
Imports System.Messaging

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

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;
        }
    }
}
#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;
ref class MyNewQueue
{
public:
   void CountLowestPriority()
   {
      
      // Holds the count of Lowest priority messages.
      UInt32 numberItems = 0;
      
      // Connect to a queue.
      MessageQueue^ myQueue = gcnew 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: {0}", numberItems
 );
      return;
   }

};

int main()
{
   
   // Create a new instance of the class.
   MyNewQueue^ myNewQueue = gcnew MyNewQueue;
   
   // Output the count of Lowest priority messages.
   myNewQueue->CountLowestPriority();
   return 0;
}

package MyProject;

import System.*;
import System.Messaging.*;

/// <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(String[]
 args)
    {
        // Create a new instance of the class.
        MyNewQueue myNewQueue = new MyNewQueue();
        // Output the count of Lowest priority messages.
        myNewQueue.CountLowestPriority();
        return;
    } //main

    //**************************************************
    // Iterates through messages in a queue and examines
    // their priority.
    //**************************************************
    public void CountLowestPriority()
    {
        // Holds the count of Lowest priority messages.
        long 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.get_MessageReadPropertyFilter().set_Priority(true);
        // Move to the next message and examine its priority.
        while (myEnumerator.MoveNext()) {
            // Increase the count if priority is Lowest.
            if (myEnumerator.get_Current().get_Priority().
                Equals(MessagePriority.Lowest)) {
                numberItems++;
            }
        }
        // Display final count.
        Console.WriteLine("Lowest priority messages: "
            + ((Int32)numberItems).ToString());
        return;
    } //CountLowestPriority
} //MyNewQueue
継承階層継承階層
System.Object
   System.MarshalByRefObject
    System.Messaging.MessageEnumerator
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
MessageEnumerator メンバ
System.Messaging 名前空間
Message クラス
MessageQueue.GetMessageEnumerator2

MessageEnumerator プロパティ


パブリック プロパティパブリック プロパティ

明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.IEnumerator.Current その列挙子が指している現在の Message を取得します。
参照参照

関連項目

MessageEnumerator クラス
System.Messaging 名前空間
Message クラス
MessageQueue.GetMessageEnumerator2

MessageEnumerator メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Close 列挙子と関連付けられたリソースを解放します。
パブリック メソッド CreateObjRef  リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 ( MarshalByRefObject から継承されます。)
パブリック メソッド Dispose オーバーロードされます。 MessageEnumerator によって使用されているリソースを解放します。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。)
パブリック メソッド GetLifetimeService  対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド InitializeLifetimeService  対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。)
パブリック メソッド MoveNext オーバーロードされます。 キュー内の次のメッセージに列挙子を進めます。
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド RemoveCurrent オーバーロードされます。 キューから現在のメッセージを削除し、そのメッセージを呼び出し元アプリケーションに返します。メッセージを削除すると、そのメッセージがキューから削除されます。
パブリック メソッド Reset キューの先頭を指すように現在の列挙子をリセットします。
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

MessageEnumerator クラス
System.Messaging 名前空間
Message クラス
MessageQueue.GetMessageEnumerator2

MessageEnumerator メンバ

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

MessageEnumerator データ型で公開されるメンバを以下の表に示します。


パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Close 列挙子と関連付けられたリソースを解放します。
パブリック メソッド CreateObjRef  リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 (MarshalByRefObject から継承されます。)
パブリック メソッド Dispose オーバーロードされます。 MessageEnumerator によって使用されているリソースを解放します。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。)
パブリック メソッド GetLifetimeService  対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド InitializeLifetimeService  対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。)
パブリック メソッド MoveNext オーバーロードされます。 キュー内の次のメッセージに列挙子を進めます。
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド RemoveCurrent オーバーロードされます。 キューから現在のメッセージを削除し、そのメッセージを呼び出し元アプリケーションに返します。メッセージを削除すると、そのメッセージがキューから削除されます。
パブリック メソッド Reset キューの先頭を指すように現在の列挙子をリセットします。
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.IEnumerator.Current その列挙子が指している現在の Message を取得します。
参照参照

関連項目

MessageEnumerator クラス
System.Messaging 名前空間
Message クラス
MessageQueue.GetMessageEnumerator2



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「MessageEnumerator」の関連用語

MessageEnumeratorのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



MessageEnumeratorのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS