Using COM you can get the MessageCount property of a queue. I can achieve the same thing with the MSMQ.Interop, but I was wondering if there is any way to do this with a pure .NET approach?
Asked
Active
Viewed 629 times
1
-
Unfortunately Microsoft screwed up here and forgot to implement a MessageQueue.MessageCount property. The only API in .NET is the .GetAllMessages() method but this offers very poor performance if you have tens of thousands of messages in your queue. The only real choices at the moment are WMI, COM, or implementing your own custom technique of enumerating and peeking at the message while incrementing a counter. Some alternative are mentioned here: http://stackoverflow.com/questions/3869022/is-there-a-way-to-check-how-many-messages-are-in-a-msmq-queue – Randy Burden May 09 '12 at 23:54
3 Answers
0
Sure! You can use LINQ it's fast and simple
int Total = 0;
using (var queue = new MessageQueue("queue_name", QueueAccessMode.ReceiveAndAdmin))
{
Total = (from Message msg in queue
select msg).Count();
}

Pavel Kovalev
- 7,521
- 5
- 45
- 67
0
Sure, using WMI:
Select * From Win32_PerfRawData_MSMQ_MSMQQueue

hichris123
- 10,145
- 15
- 56
- 70

lod3n
- 2,893
- 15
- 16
-
Unfortunately when using this method the queue names get truncated to 64 characters which for most, can make this option unusable. See: http://connect.microsoft.com/VisualStudio/feedback/details/91525/system-daignostics-performancecounter-fails-when-using-instance-name-64-characters – Randy Burden May 10 '12 at 00:03
0
this is fastest method to calculate message inside private queues.
//using System.Diagnostics
int messageCount =0;
private MessageQueue[] privatequeuelist = MessageQueue.GetPrivateQueuesByMachine(Dns.GetHostName());
foreach(var Queue in MessageQueue)
{
string fullyQualifiedQueueName = string.Format(@"{0}\{1}", Environment.MachineName, Queue.QueueName);
PerformanceCounterCategory category = new PerformanceCounterCategory("MSMQ Queue");
PerformanceCounter cntr = new PerformanceCounter("MSMQ Queue", "Messages in Queue");
if (category.InstanceExists(fullyQualifiedQueueName.ToLower()))
{
cntr.InstanceName = fullyQualifiedQueueName.ToLower(CultureInfo.CurrentCulture);
messageCount = (int)cntr.NextValue();
}
Console.WriteLine(Queue.QueueName + "===" + messageCount);
}
Console.ReadLine();

rhatwar007
- 343
- 3
- 14