Number of messages on a MessageQueue

How do I find the number of messages currently on a MessageQueue?
LVL 13
josgoodAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.