alexgman
7/15/2015 - 5:10 PM

gistfile1.cs

    static class MessageQueueExt
    {
        /// <summary>
        /// Return number of messages in the MSMQ.
        /// from: http://stackoverflow.com/questions/3869022/is-there-a-way-to-check-how-many-messages-are-in-a-msmq-queue
        /// </summary>
        public static int Count(this MessageQueue queue)
        {
            var queueCounter = new System.Diagnostics.PerformanceCounter(
            "MSMQ Queue", 
            "Messages in Queue",
            queue.MachineName + @"\" + queue.QueueName);

            var result = queueCounter.NextValue();
            return int.Parse(result.ToString());
        }
    }