• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 725
  • Last Modified:

Number of messages on a MessageQueue

How do I find the number of messages currently on a MessageQueue?
0
josgood
Asked:
josgood
1 Solution
 
Expert1701Commented:
Quoted from, http://community.sgdotnet.org/forums/thread/18308.aspx:

"Unfortunately, the current managed MSMQ APIs (System.Messaging) does not support the MSMQ admin APIs yet (not even in .NET 2.0). So, basically we have three options:

1. Use the performance counters API to get the counters. The following code will do the trick:

  using System.Diagnostics;
  PerformanceCounterCategory myCat = new PerformanceCounterCategory("MSMQ Queue");
  PerformanceCounter cntr = new PerformanceCounter();
  cntr.CategoryName = "MSMQ Queue";
  cntr.CounterName = "Messages in Queue";

  foreach (string inst in myCat.GetInstanceNames())
  {
    cntr.InstanceName = inst;
    Console.Write(inst + " = ");
    Console.WriteLine(cntr.NextValue().ToString());
  }

2. Using COM interop and MSMQ object. The following code will do it, after adding a reference to Microsoft Message Queue 3.0 object library:

  using System.Windows.Forms; // for SystemInformation
  object oMissing = Type.Missing;
  object oMachine = SystemInformation.ComputerName;
  MSMQ.MSMQApplicationClass msmqApp = new MSMQ.MSMQApplicationClass();

  foreach (object oFormat in (object[])msmqApp.ActiveQueues)
  {
    MSMQ.MSMQManagementClass qMgmt = new MSMQ.MSMQManagementClass();
    object oFormatName = oFormat; // oFormat is read only and we need to use ref
    qMgmt.Init(ref oMachine, ref oMissing, ref oFormatName);
    Console.WriteLine(oFormatName.ToString() + " = " + qMgmt.MessageCount.ToString());
  }

3. This method is more reliable than performance counters, but works on MSMQ 3.0 only.

  Calling MQMgmtGetInfo directly to get the active queues (PROPID_MGMT_MSMQ_ACTIVEQUEUES) if needed, and to get the number of messages in each queue (PROPID_MGMT_QUEUE_BYTES_IN_QUEUE). Marshaling the parameters to this call (especially the third one, MQMGMTPROPS structure may be a little tricky, so I was too lazy to write a sample this time."
0
 
josgoodAuthor Commented:
Thank you very much for your excellent answer!  I am impressed with your completeness -- you clearly deserve your handle.

I''l try answer #3 first and will respond again soon.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now