interviews.dotnetthread.com

↑ Grab this Headline Animator

Monday, August 17, 2009

To get the count of messages in a MSMQ Queue in C#.NET

There is no direct property provided with MessageQueue class.
We can use GetMessageEnumerator2 to get the enumerator to enumerate and get the count of number of messages present in the queue.

The below code is used for getting the messages count from a transactional queues


public static int GetMessageCount(MessageQueue messageQueue)
{
int count = 0;
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
MessageEnumerator enumerator = messageQueue.GetMessageEnumerator2();
while (enumerator.MoveNext(new TimeSpan(0, 0, 0)))
{
count++;
}
}

return count;
}


The below code is used for getting the messages count from a non transactional queues


public static int GetMessageCount(MessageQueue messageQueue)
{
int count = 0;
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
MessageEnumerator enumerator = messageQueue.GetMessageEnumerator2();
while (enumerator.MoveNext(new TimeSpan(0, 0, 0)))
{
count++;
}
}

return count;
}

Submit this story to DotNetKicks

1 comment:

rahul said...

Thanks for gr8 information.
I found good resources of c#. Check this out

http://CSharpTalk.com

Post a Comment

Post your comments/questions/feedback for this Article.

 

Latest Articles