Number of messages on a MessageQueue

Posted on 2006-04-05
Last Modified: 2012-06-27
How do I find the number of messages currently on a MessageQueue?
Question by:josgood
    LVL 11

    Accepted Solution

    Quoted from,

    "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 + " = ");

    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."
    LVL 13

    Author Comment

    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.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now