MessageEnumerator 类

定义

提供一个只进游标,用于枚举消息队列中的消息。

public ref class MessageEnumerator : MarshalByRefObject, IDisposable, System::Collections::IEnumerator
public class MessageEnumerator : MarshalByRefObject, IDisposable, System.Collections.IEnumerator
type MessageEnumerator = class
    inherit MarshalByRefObject
    interface IEnumerator
    interface IDisposable
Public Class MessageEnumerator
Inherits MarshalByRefObject
Implements IDisposable, IEnumerator
继承
MessageEnumerator
实现

示例

以下示例获取队列中的消息动态列表,并将属性设置为 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 一个数组,该数组在调用指定方法时包含给定即时副本(快照)队列。

与静态快照不同,枚举器允许修改集合。 使用 a MessageEnumerator,可以从队列中删除消息,更改会立即反映在队列中。

查询队列时,枚举器不会从队列中删除消息。 它在当前游标位置返回有关消息的信息,但它将消息保留在队列中。

A MessageEnumerator 是一个游标,初始化为动态列表的头。 根据消息优先级,列表顺序与队列中的消息顺序相同。 可以通过调用 MoveNext将光标移到队列中的第一条消息。 初始化枚举器后,可以使用 MoveNext 该枚举器单步执行剩余消息。 可以通过将超时 MoveNext 传递给方法来指定是否等待消息可用。

由于枚举器是动态的,因此,由于低优先级) ,可以由枚举器访问超出游标当前位置 (追加的消息。 无法访问游标当前位置之前插入的消息。 无法向后退。MessageEnumerator 游标允许仅向前移动。 使用此方法 Reset 可将光标放回队列的开头。

MessageEnumerator给定队列的实例独立工作。 可以创建两 MessageEnumerator 个应用于同一队列的实例。 如果第二个枚举器位于第一个枚举器之前,队列中消息所做的更改 MessageEnumerator 将立即反映在第二个枚举器中。 但是,如果两个枚举器具有相同的位置,其中一个枚举器删除该位置的消息,则当其他枚举器尝试获取当前删除的消息上的 Current 属性值时,将引发异常。

备注

If you create an instance of MessageQueue with MessageQueue.DenySharedReceive set to true, no other application can modify the messages in your enumerator while you have the connection to the queue.

属性

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

适用于

另请参阅