MessageQueue.GetMessageEnumerator 方法

定義

警告

This method returns a MessageEnumerator that implements RemoveCurrent family of methods incorrectly. Please use GetMessageEnumerator2 instead.

警告

此 API 現已淘汰。

為佇列中的所有訊息建立列舉值物件。Creates an enumerator object for all the messages in the queue. GetMessageEnumerator() 已被取代。GetMessageEnumerator() is deprecated. 應改用 GetMessageEnumerator2()GetMessageEnumerator2() should be used instead.

public:
 System::Messaging::MessageEnumerator ^ GetMessageEnumerator();
public System.Messaging.MessageEnumerator GetMessageEnumerator ();
[System.Obsolete("This method returns a MessageEnumerator that implements RemoveCurrent family of methods incorrectly. Please use GetMessageEnumerator2 instead.")]
public System.Messaging.MessageEnumerator GetMessageEnumerator ();
[System.Obsolete]
public System.Messaging.MessageEnumerator GetMessageEnumerator ();
member this.GetMessageEnumerator : unit -> System.Messaging.MessageEnumerator
[<System.Obsolete("This method returns a MessageEnumerator that implements RemoveCurrent family of methods incorrectly. Please use GetMessageEnumerator2 instead.")>]
member this.GetMessageEnumerator : unit -> System.Messaging.MessageEnumerator
[<System.Obsolete>]
member this.GetMessageEnumerator : unit -> System.Messaging.MessageEnumerator
Public Function GetMessageEnumerator () As MessageEnumerator

傳回

MessageEnumerator

MessageEnumerator,保有在佇列中所包含的訊息。The MessageEnumerator holding the messages that are contained in the queue.

屬性

範例

下列程式碼範例會取得佇列中的訊息動態清單,並計算 Priority 屬性設為的所有訊息 MessagePriority.LowestThe following code 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

備註

GetMessageEnumerator 建立佇列中所有訊息的動態清單。GetMessageEnumerator creates a dynamic list of all the messages in a queue. 您可以藉由呼叫傳回的來從佇列中移除訊息,此訊息位於列舉值的目前位置 RemoveCurrent MessageEnumerator GetMessageEnumeratorYou can remove from the queue the message at the enumerator's current position by calling RemoveCurrent for the MessageEnumerator that GetMessageEnumerator returns.

因為資料指標與佇列中的訊息動態清單相關聯,所以如果訊息超出目前的資料指標位置,則列舉會反映您對佇列中的訊息所做的任何修改。Because the cursor is associated with the dynamic list of messages in the queue, the enumeration reflects any modification you make to the messages in the queue, if the message is beyond the current cursor position. 例如,列舉值可以自動存取放置在資料指標目前位置之外的低優先順序訊息,而不是在該位置之前插入較高優先順序的訊息。For example, the enumerator can automatically access a lower-priority message placed beyond the cursor's current position, but not a higher-priority message inserted before that position. 不過,您可以藉由呼叫,重設列舉,藉此將資料指標移回清單的開頭 Reset MessageEnumeratorHowever, you can reset the enumeration, thereby moving the cursor back to the beginning of the list, by calling Reset for the MessageEnumerator.

列舉中的訊息順序反映其在佇列中的順序,因此較高優先順序的訊息會在優先順序較低的訊息之前出現。The order of the messages in the enumeration reflects their order in the queue, so higher-priority messages will appear before lower-priority ones.

如果您想要佇列中的訊息的靜態快照集,而不是動態連接到這些訊息,請呼叫 GetAllMessagesIf you want a static snapshot of the messages in the queue rather than a dynamic connection to them, call GetAllMessages. 這個方法會傳回物件的陣列 Message ,代表呼叫方法時的訊息。This method returns an array of Message objects, which represent the messages at the time the method was called.

下表顯示此方法是否適用于各種工作組模式。The following table shows whether this method is available in various Workgroup modes.

工作組模式Workgroup mode 可用Available
本機電腦Local computer Yes
本機電腦和直接格式名稱Local computer and direct format name Yes
遠端電腦Remote computer Yes
遠端電腦和直接格式名稱Remote computer and direct format name Yes

適用於

另請參閱