In MSMQ on .NET, I'm using a MessageEnumerator to look through all the messages in the queue. I want to remove messages that meet a certain condition.
When I call MoveNext to step through the queue, I get back a boolean to tell me if the current message exists. But, when I do a RemoveCurrent, how do I know if the current message after the removal exists? Is the only way to check Current and handle the exception?
Here is an example where I use the simple condition of removing messages that are over one year old. Assume the queue was created elsewhere and set with MessageReadPropertyFilter.ArrivedTime = true:
private void RemoveOldMessages(MessageQueue q)
{
MessageEnumerator me = q.GetMessageEnumerator2();
bool hasMessage = me.MoveNext();
while (hasMessage)
{
if (me.Current.ArrivedTime < DateTime.Now.AddYears(-1))
{
Message m = me.RemoveCurrent(new TimeSpan(1));
// Removes and returns the current message
// then moves to the cursor to the next message
// How do I know if there is another message? Is this right?:
try
{
m = me.Current;
hasMessage = true;
}
catch (MessageQueueException ex)
{
hasMessage = false;
}
}
else
{
hasMessage = me.MoveNext();
// MoveNext returns a boolean to let me know if is another message
}
}
}
Albeit belated:
Every call to RemoveCurrent() will invalidate your Enumerator. As such you need to call Reset() to keep starting at the beginning of the Enumerator (Message Queue).
private void RemoveOldMessages(MessageQueue q)
{
MessageEnumerator me = q.GetMessageEnumerator2();
while (me.MoveNext())
{
try
{
Message m = me.RemoveCurrent();
// Handle Message
}
finally
{
me.Reset();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With