MessageQueue.GetMessageEnumerator 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
注意
This method returns a MessageEnumerator that implements RemoveCurrent family of methods incorrectly. Please use GetMessageEnumerator2 instead.
为队列中的所有消息创建枚举数对象。 GetMessageEnumerator() 已弃用。 应改用 GetMessageEnumerator2()。
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 ();
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
Public Function GetMessageEnumerator () As MessageEnumerator
返回
该 MessageEnumerator 包括队列中所包含的消息。
- 属性
示例
下面的代码示例获取队列中消息的动态列表,并将 属性设置为 MessagePriority.Lowest的所有消息Priority进行计数。
#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 创建队列中所有消息的动态列表。 可以通过调用 RemoveCurrentMessageEnumeratorGetMessageEnumerator 返回的 从队列中删除枚举器当前位置的消息。
由于游标与队列中消息的动态列表相关联,因此如果消息超出当前游标位置,则枚举将反映对队列中消息所做的任何修改。 例如,枚举器可以自动访问放置在光标当前位置之外的低优先级消息,但不能访问在该位置之前插入的较高优先级消息。 但是,可以通过调用 ResetMessageEnumerator来重置枚举,从而将光标移回列表的开头。
枚举中消息的顺序反映了它们在队列中的顺序,因此优先级较高的消息将出现在优先级较低的消息之前。
如果需要队列中消息的静态快照而不是动态连接,请调用 GetAllMessages。 此方法返回对象数组 Message ,这些对象表示调用方法时的消息。
下表显示了此方法是否在各种工作组模式下可用。
工作组模式 | 可用 |
---|---|
本地计算机 | 是 |
本地计算机和直接格式名称 | 是 |
远程计算机 | 是 |
远程计算机和直接格式名称 | 是 |