Export (0) Print
Expand All

MessageEnumerator Class

Provides a forward-only cursor to enumerate through messages in a message queue.

Namespace: System.Messaging
Assembly: System.Messaging (in system.messaging.dll)

public class MessageEnumerator : MarshalByRefObject, IEnumerator, IDisposable
public class MessageEnumerator extends MarshalByRefObject implements IEnumerator, IDisposable
public class MessageEnumerator extends MarshalByRefObject implements IEnumerator, IDisposable
Not applicable.

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.

NoteNote:

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.

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;
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;
		}
	}
}

package MyProject;

import System.*;
import System.Messaging.*;

/// <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(String[] args)
    {
        // Create a new instance of the class.
        MyNewQueue myNewQueue = new MyNewQueue();
        // Output the count of Lowest priority messages.
        myNewQueue.CountLowestPriority();
        return;
    } //main

    //**************************************************
    // Iterates through messages in a queue and examines
    // their priority.
    //**************************************************
    public void CountLowestPriority()
    {
        // Holds the count of Lowest priority messages.
        long 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.get_MessageReadPropertyFilter().set_Priority(true);
        // Move to the next message and examine its priority.
        while (myEnumerator.MoveNext()) {
            // Increase the count if priority is Lowest.
            if (myEnumerator.get_Current().get_Priority().
                Equals(MessagePriority.Lowest)) {
                numberItems++;
            }
        }
        // Display final count.
        Console.WriteLine("Lowest priority messages: "
            + ((Int32)numberItems).ToString());
        return;
    } //CountLowestPriority
} //MyNewQueue

System.Object
   System.MarshalByRefObject
    System.Messaging.MessageEnumerator

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft