MessageEnumerator 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供順向資料指標,進而列舉訊息佇列中的訊息。
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
- 繼承
- 實作
範例
下列範例會取得佇列中的訊息動態清單,並計算屬性設定為 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
備註
用於 MessageEnumerator 與佇列中的訊息動態互動。 透過 MessageQueue 類別提供的方法可以傳回 MessageEnumerator 指向佇列中訊息的動態清單,或陣列,其中包含指定瞬間的複本 - 快照集 - 呼叫指定方法時佇列的快照集。
不同于靜態快照集,列舉值可讓您修改集合。 MessageEnumerator使用 ,您可以從佇列中移除訊息,變更會立即反映在佇列中。
列舉值不會在查詢佇列時,從佇列中移除訊息。 它會傳回目前游標位置之訊息的相關資訊,但會將訊息保留在佇列中。
MessageEnumerator是資料指標,初始化為動態清單的前端。 根據訊息優先順序,清單順序與佇列中的訊息順序相同。 您可以呼叫 MoveNext ,將游標移至佇列中的第一個訊息。 初始化列舉值之後,您可以使用 MoveNext 來逐步執行其餘的訊息。 您可以指定是否要將逾時傳遞至 MoveNext 方法,以等候訊息變成可用狀態。
由於列舉值是動態的,因此可以透過列舉值存取超出資料指標目前位置的訊息 (例如,因為優先順序偏低) 。 無法存取資料指標目前位置之前插入的訊息。 無法向後 MessageEnumerator 執行 。 游標允許順向移動。 方法 Reset 可讓您將游標放回佇列的開頭。
MessageEnumerator指定佇列的實例會獨立運作。 您可以建立兩 MessageEnumerator 個套用至相同佇列的實例。 如果第二個列舉值位於第一個列舉值之前,佇列中訊息所做的變更 MessageEnumerator 將會立即反映在第二個列舉值中。 不過,如果兩個列舉值的位置相同,其中一個列舉值會移除該位置的訊息,如果另一個列舉值嘗試取得 Current 目前刪除之訊息上的 屬性值,就會擲回例外狀況。
注意
如果您建立 的實例 MessageQueue ,且 MessageQueue.DenySharedReceive 設定 true
為 ,則當您有佇列連線時,其他應用程式就無法修改列舉值中的訊息。
屬性
Current |
取得這個列舉值指向的目前 Message。 |
CursorHandle |
取得用來瀏覽佇列中訊息的原生 (Native) 訊息佇列資料指標控制代碼。 |
方法
Close() |
釋放與列舉值相關的資源。 |
CreateObjRef(Type) |
建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。 (繼承來源 MarshalByRefObject) |
Dispose() |
釋放 MessageEnumerator 所使用的所有資源。 |
Dispose(Boolean) |
釋放 MessageEnumerator 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
Finalize() |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 釋放列舉值所使用的資源。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetLifetimeService() |
已過時。
擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。 (繼承來源 MarshalByRefObject) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
InitializeLifetimeService() |
已過時。
取得存留期服務物件,以控制這個執行個體的存留期原則。 (繼承來源 MarshalByRefObject) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
MemberwiseClone(Boolean) |
建立目前 MarshalByRefObject 物件的淺層複本。 (繼承來源 MarshalByRefObject) |
MoveNext() |
如果目前有可用的列舉值,讓列舉值前進至佇列中的下一個訊息。 |
MoveNext(TimeSpan) |
讓列舉值前進至佇列中的下一個訊息。 如果列舉值已位於佇列的結尾,MoveNext() 會一直等待,直到有可用的訊息或指定的逾時到期為止。 |
RemoveCurrent() |
從異動或非異動性佇列中移除目前的訊息,並將訊息傳回至呼叫的應用程式。 沒有為訊息到達佇列指定逾時。 |
RemoveCurrent(MessageQueueTransaction) |
從異動佇列中移除目前的訊息,並將訊息傳回至呼叫的應用程式。 沒有為訊息到達佇列指定逾時。 |
RemoveCurrent(MessageQueueTransactionType) |
從佇列移除目前的訊息,並將訊息傳回至呼叫的應用程式。 沒有為訊息到達佇列指定逾時。 |
RemoveCurrent(TimeSpan) |
從佇列中移除目前的訊息,並將訊息傳回至呼叫的應用程式。 如果有要移除的訊息,方法會立即將它傳回。 否則,方法會為新訊息的到達等待指定的逾時。 |
RemoveCurrent(TimeSpan, MessageQueueTransaction) |
從異動佇列中移除目前的訊息,並將訊息傳回至呼叫的應用程式。 如果有要移除的訊息,方法會立即將它傳回。 否則,方法會為新訊息的到達等待指定的逾時。 |
RemoveCurrent(TimeSpan, MessageQueueTransactionType) |
從佇列移除目前的訊息,並將訊息傳回至呼叫的應用程式。 如果有要移除的訊息,方法會立即將它傳回。 否則,方法會為新訊息的到達等待指定的逾時。 |
Reset() |
重設目前的列舉值,讓它指向佇列的前端。 |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
明確介面實作
IEnumerator.Current |
傳回 Message,其參考目前游標位置的訊息。 |