MessageEnumerator 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供一个只进游标,用于枚举消息队列中的消息。
public ref class MessageEnumerator : MarshalByRefObject, IDisposable, System::Collections::IEnumeratorpublic class MessageEnumerator : MarshalByRefObject, IDisposable, System.Collections.IEnumeratortype MessageEnumerator = class
    inherit MarshalByRefObject
    interface IEnumerator
    interface IDisposablePublic Class MessageEnumerator
Inherits MarshalByRefObject
Implements IDisposable, IEnumerator- 继承
- 实现
示例
以下示例获取队列中消息的动态列表,并将 属性设置为 MessagePriority.Lowest的所有消息Priority计数。
#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;
}
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;
        }
    }
}
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
        
        ' 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
End Class
注解
用于 MessageEnumerator 与队列中的消息进行动态交互。 通过 MessageQueue 类提供的方法可以返回指向MessageEnumerator队列中消息的动态列表,也可以返回一个数组,该数组包含给定时刻的副本(调用指定方法时队列的快照)。
与静态快照不同,枚举器允许修改集合。 使用 , MessageEnumerator可以从队列中删除消息,更改会立即反映在队列中。
枚举器在查询队列时不会从队列中删除消息。 它返回当前游标位置处的消息的相关信息,但会将消息保留在队列中。
是 MessageEnumerator 一个游标,初始化为动态列表的头。 根据消息优先级,列表顺序与队列中消息的顺序相同。 可以通过调用 MoveNext将光标移动到队列中的第一条消息。 初始化枚举器后,可以使用 MoveNext 向前单步执行剩余消息。 可以通过将超时 MoveNext 传递到 方法来指定是否等待消息变得可用。
由于枚举器是动态的,因此枚举器可以访问追加到游标当前位置之外的消息 (例如,由于低优先级) 。 在无法访问光标当前位置之前插入的消息。 不能使用 MessageEnumerator后退。 光标允许仅向前移动。 使用 Reset 方法可将游标放回队列的开头。
给定队列的 实例 MessageEnumerator 独立工作。 可以创建两 MessageEnumerator 个应用于同一队列的实例。 如果第二个 MessageEnumerator 枚举器位于第一个枚举器之前,则对队列中的消息所做的更改将立即反映在第二个枚举器中。 但是,如果两个枚举器具有相同的位置,并且其中一个枚举器删除位于该位置的消息,则当另一个枚举器尝试获取 Current 现在删除的消息上的 属性值时,将引发异常。
注意
如果创建 的实例 MessageQueue ,并将 MessageQueue.DenySharedReceive 设置为 true,则当您与队列建立连接时,任何其他应用程序都不能修改枚举器中的消息。
属性
| Current | 获取该枚举数指向的当前 Message。 | 
| CursorHandle | 获取用于浏览队列消息的本机消息队列游标句柄。 | 
方法
| Close() | 释放与枚举数关联的资源。 | 
| CreateObjRef(Type) | 创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。(继承自 MarshalByRefObject) | 
| Dispose() | 释放由 MessageEnumerator 使用的所有资源。 | 
| Dispose(Boolean) | 释放由 MessageEnumerator 占用的非托管资源,还可以另外再释放托管资源。 | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| Finalize() | 此 API 支持产品基础结构,不能在代码中直接使用。 释放枚举数控制的资源。 | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetLifetimeService() | 
		已过时.
	 检索控制此实例的生存期策略的当前生存期服务对象。(继承自 MarshalByRefObject) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| InitializeLifetimeService() | 
		已过时.
	 获取生存期服务对象来控制此实例的生存期策略。(继承自 MarshalByRefObject) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| MemberwiseClone(Boolean) | 创建当前 MarshalByRefObject 对象的浅表副本。(继承自 MarshalByRefObject) | 
| MoveNext() | 如果当前队列中有下一条可用的消息,则使枚举数前进到该消息。 | 
| MoveNext(TimeSpan) | 使枚举数前进到队列中的下一条消息。 如果枚举数位于队列的末尾,则 MoveNext() 将一直等到消息可用或给定 timeout 到期为止。 | 
| RemoveCurrent() | 从事务性或非事务性队列中移除当前消息,并将该消息返回给调用应用程序。 对于消息到达队列的用时没有超时指定。 | 
| RemoveCurrent(MessageQueueTransaction) | 从事务性队列中移除当前消息,并将该消息返回给调用应用程序。 对于消息到达队列的用时没有超时指定。 | 
| RemoveCurrent(MessageQueueTransactionType) | 从队列中移除当前消息并将该消息返回给调用应用程序。 对于消息到达队列的用时没有超时指定。 | 
| RemoveCurrent(TimeSpan) | 从队列中移除当前消息并将该消息返回给调用应用程序。 如果有要移除的消息,该方法将立即返回该消息。 否则,该方法在指定的超时内等待新消息到达。 | 
| RemoveCurrent(TimeSpan, MessageQueueTransaction) | 从事务性队列中移除当前消息,并将该消息返回给调用应用程序。 如果有要移除的消息,该方法将立即返回该消息。 否则,该方法在指定的超时内等待新消息到达。 | 
| RemoveCurrent(TimeSpan, MessageQueueTransactionType) | 从队列中移除当前消息并将该消息返回给调用应用程序。 如果有要移除的消息,该方法将立即返回该消息。 否则,该方法在指定的超时内等待新消息到达。 | 
| Reset() | 重置当前枚举数,使其指向队列开头。 | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| IEnumerator.Current | 返回引用当前游标位置的消息的 Message。 |