MessageEnumerator 类

定义

提供一个只进游标,以枚举通过消息队列中的消息。Provides a forward-only cursor to enumerate through messages in a message queue.

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
实现

示例

下面的示例获取队列中消息的动态列表, 并对属性设置为Priority MessagePriority.Lowest的所有消息进行计数。The following example gets a dynamic list of messages in a queue and counts all messages with the Priority property set to MessagePriority.Lowest.

#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

注解

用于与队列中的消息进行动态交互。MessageEnumeratorUse MessageEnumerator for dynamic interaction with messages in a queue. 通过MessageQueue类提供的MessageEnumerator方法可以返回指向队列中的消息动态列表的, 或返回在调用指定的方法时队列的给定即时快照上包含副本的数组。Methods available through the MessageQueue class can return either a MessageEnumerator pointing to a dynamic list of messages in the queue, or an array that contains a copy at a given instant - a snapshot - of the queue at the time the specified method was called.

与静态快照不同, 枚举器允许您修改集合。Unlike a static snapshot, an enumerator allows you to modify the collection. MessageEnumerator使用, 可以从队列中删除消息, 而更改会立即反映在队列中。Using a MessageEnumerator, you can remove messages from the queue, and the change is immediately reflected in the queue.

枚举器在查询队列时不会从队列中删除消息。An enumerator does not remove the messages from the queue when it queries the queue. 它返回有关当前光标位置处的消息的信息, 但它将消息保留在队列中。It returns information about the message at the current cursor position, but it leaves the message in the queue.

MessageEnumerator是游标, 初始化为动态列表的开头。A MessageEnumerator is a cursor, initialized to the head of a dynamic list. 列表的顺序与消息在队列中的顺序相同, 具体取决于消息的优先级。The list order is the same as the order of the messages in the queue, according to message priority. 通过调用MoveNext, 可以将光标移到队列中的第一条消息。You can move the cursor to the first message in the queue by calling MoveNext. 初始化枚举器后, 可以使用MoveNext前进到其余的消息。After the enumerator has been initialized, you can use MoveNext to step forward through the remaining messages. 可以通过将MoveNext超时传递到方法来指定是否等待消息变为可用。You can specify whether to wait for a message to become available by passing a timeout into the MoveNext method.

由于枚举器是动态的, 因此枚举器可以访问附加在游标当前位置之外的消息 (例如, 由于低优先级)。Because the enumerator is dynamic, a message that is appended beyond the cursor's current position (for example, due to low priority), can be accessed by the enumerator. 无法访问在游标的当前位置之前插入的消息。A message that is inserted before the cursor's current position cannot be accessed. 不可能向后移动MessageEnumeratorIt is not possible to step backward with a MessageEnumerator. 游标允许只进移动。A cursor allows forward-only movement. Reset方法使您能够将游标放回队列的开头。The Reset method enables you to place the cursor back at the beginning of the queue.

给定队列MessageEnumerator的实例是独立工作的。Instances of MessageEnumerator for a given queue work independently. 您可以创建适用MessageEnumerator于同一队列的两个实例。You can create two MessageEnumerator instances that apply to the same queue. 如果第二个MessageEnumerator枚举器定位在第一个枚举数之前, 则会立即在第二个枚举器中反映对队列中的消息所做的更改。The changes that one MessageEnumerator makes to the messages in the queue will be reflected immediately in a second enumerator if the second enumerator is positioned before the first. 但是, 如果两个枚举器具有相同的位置, 并且其中一个枚举器删除了该位置的消息, 则如果其他枚举器尝试获取现已删除消息Current的属性值, 则将引发异常。However, if two enumerators have the same position and one of them removes the message at that position, an exception is thrown if the other enumerator attempts to get the value of the Current property on the now-deleted message.

备注

如果创建的实例MessageQueue , 并MessageQueue.DenySharedReceive将设置为true, 则在连接到队列时, 其他应用程序不能修改枚举器中的消息。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

获取该枚举数指向的当前 MessageGets the current Message that this enumerator points to.

CursorHandle

获取用于浏览队列消息的本机消息队列游标句柄。Gets the native Message Queuing cursor handle used to browse messages in the queue.

方法

Close()

释放与枚举数关联的资源。Frees the resources associated with the enumerator.

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
Dispose()

释放由 MessageEnumerator 使用的所有资源。Releases all resources used by the MessageEnumerator.

Dispose(Boolean)

释放由 MessageEnumerator 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the MessageEnumerator and optionally releases the managed resources.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Finalize()

释放枚举数控制的资源。Releases the resources held by the enumerator.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)
MoveNext()

如果当前队列中有下一条可用的消息,则使枚举数前进到该消息。Advances the enumerator to the next message in the queue, if one is currently available.

MoveNext(TimeSpan)

使枚举数前进到队列中的下一条消息。Advances the enumerator to the next message in the queue. 如果枚举数位于队列的末尾,则 MoveNext() 将一直等到消息可用或给定 timeout 到期为止。If the enumerator is positioned at the end of the queue, MoveNext() waits until a message is available or the given timeout expires.

RemoveCurrent()

从事务性或非事务性队列中移除当前消息,并将该消息返回给调用应用程序。Removes the current message from a transactional or non-transactional queue and returns the message to the calling application. 对于消息到达队列的用时没有超时指定。There is no timeout specified for a message to arrive in the queue.

RemoveCurrent(MessageQueueTransaction)

从事务性队列中移除当前消息,并将该消息返回给调用应用程序。Removes the current message from a transactional queue and returns the message to the calling application. 对于消息到达队列的用时没有超时指定。There is no timeout specified for a message to arrive in the queue.

RemoveCurrent(MessageQueueTransactionType)

从队列中移除当前消息并将该消息返回给调用应用程序。Removes the current message from a queue and returns the message to the calling application. 对于消息到达队列的用时没有超时指定。There is no timeout specified for a message to arrive in the queue.

RemoveCurrent(TimeSpan)

从队列中移除当前消息并将该消息返回给调用应用程序。Removes the current message from the queue and returns the message to the calling application. 如果有要移除的消息,该方法将立即返回该消息。If there is a message to remove, the method returns it immediately. 否则,该方法在指定的超时内等待新消息到达。Otherwise, the method waits the specified timeout for a new message to arrive.

RemoveCurrent(TimeSpan, MessageQueueTransaction)

从事务性队列中移除当前消息,并将该消息返回给调用应用程序。Removes the current message from a transactional queue and returns the message to the calling application. 如果有要移除的消息,该方法将立即返回该消息。If there is a message to remove, the method returns it immediately. 否则,该方法在指定的超时内等待新消息到达。Otherwise, the method waits the specified timeout for a new message to arrive.

RemoveCurrent(TimeSpan, MessageQueueTransactionType)

从队列中移除当前消息并将该消息返回给调用应用程序。Removes the current message from a queue and returns the message to the calling application. 如果有要移除的消息,该方法将立即返回该消息。If there is a message to remove, the method returns it immediately. 否则,该方法在指定的超时内等待新消息到达。Otherwise, the method waits the specified timeout for a new message to arrive.

Reset()

重置当前枚举数,使其指向队列开头。Resets the current enumerator so it points to the head of the queue.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

IEnumerator.Current

返回引用当前游标位置的消息的 MessageReturns a Message that references the message at the current cursor position.

适用于

另请参阅