MessageEnumerator MessageEnumerator MessageEnumerator MessageEnumerator Class

Definition

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
Inheritance
MessageEnumeratorMessageEnumeratorMessageEnumeratorMessageEnumerator
Implements

Examples

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
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 'Main


        
        ' 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 'CountLowestPriority

End Class 'MyNewQueue

Remarks

Use MessageEnumerator for dynamic interaction with messages in a queue. 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. 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.

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. You can move the cursor to the first message in the queue by calling MoveNext. After the enumerator has been initialized, you can use MoveNext to step forward through the remaining messages. 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. It is not possible to step backward with a MessageEnumerator. A cursor allows forward-only movement. The Reset method enables you to place the cursor back at the beginning of the queue.

Instances of MessageEnumerator for a given queue work independently. You can create two MessageEnumerator instances that apply to the same queue. 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. 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.

Note

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.

Properties

Current Current Current Current

Gets the current Message that this enumerator points to.

CursorHandle CursorHandle CursorHandle CursorHandle

Gets the native Message Queuing cursor handle used to browse messages in the queue.

Methods

Close() Close() Close() Close()

Frees the resources associated with the enumerator.

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose() Dispose() Dispose() Dispose()

Releases all resources used by the MessageEnumerator.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

Releases the unmanaged resources used by the MessageEnumerator and optionally releases the managed resources.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize() Finalize() Finalize() Finalize()
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
MoveNext() MoveNext() MoveNext() MoveNext()

Advances the enumerator to the next message in the queue, if one is currently available.

MoveNext(TimeSpan) MoveNext(TimeSpan) MoveNext(TimeSpan) MoveNext(TimeSpan)

Advances the enumerator to the next message in the queue. If the enumerator is positioned at the end of the queue, MoveNext() waits until a message is available or the given timeout expires.

RemoveCurrent() RemoveCurrent() RemoveCurrent() 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) RemoveCurrent(MessageQueueTransaction) RemoveCurrent(MessageQueueTransaction) 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) RemoveCurrent(MessageQueueTransactionType) RemoveCurrent(MessageQueueTransactionType) 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) RemoveCurrent(TimeSpan) RemoveCurrent(TimeSpan) 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) RemoveCurrent(TimeSpan, MessageQueueTransaction) RemoveCurrent(TimeSpan, MessageQueueTransaction) 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) RemoveCurrent(TimeSpan, MessageQueueTransactionType) RemoveCurrent(TimeSpan, MessageQueueTransactionType) 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() Reset() Reset() Reset()

Resets the current enumerator so it points to the head of the queue.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

IEnumerator.Current IEnumerator.Current IEnumerator.Current IEnumerator.Current

Returns a Message that references the message at the current cursor position.

Applies to

See Also