MessageEnumerator 類別

定義

提供順向資料指標 (Cursor),以列舉訊息佇列中的訊息。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. 不可能以回溯方式MessageEnumerator執行。It 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

取得用來瀏覽佇列中訊息的原生 (Native) 訊息佇列資料指標控制代碼。Gets the native Message Queuing cursor handle used to browse messages in the queue.

方法

Close()

釋放與列舉值相關的資源。Frees the resources associated with the enumerator.

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。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()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。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 的淺層複本 (Shallow Copy)。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() 會一直等待,直到有可用的訊息或指定的逾時到期為止。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()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

IEnumerator.Current

傳回 Message,其參考目前游標位置的訊息。Returns a Message that references the message at the current cursor position.

適用於

另請參閱