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 表示呼叫方法時所傳回的訊息。
下表顯示此方法是否可在各種工作組模式中使用。
工作組模式 | 可用 |
---|---|
本機電腦 | 是 |
本機計算機和直接格式名稱 | 是 |
遠端電腦 | 是 |
遠端電腦和直接格式名稱 | 是 |