0

So I am busy writing an Watchdog for message queue installed on one of my servers. I have a application ( 3rd party ) that is listening on the queue and processing the messages. I now want to do a count on it and if message reach example 1500 I send a email. So all my code works except that I need to close the 3rd party app to use the message queue. What I do is I get all the queue names that exist. Work fine.

public void GetPrivateQueues()
{
    MessageQueue[] QueueList =
    MessageQueue.GetPrivateQueuesByMachine(".");

    foreach (MessageQueue queueItem in QueueList)
    {
        i++;

        myPrivateQueues.Add(queueItem.Path);

        Count(queueItem.Path);
    }

    return;
}

So when I do the count of the queue like this

public void Count(String path)
{

    MessageQueue queue = new MessageQueue(path);
    MessageEnumerator messageEnumerator = queue.GetMessageEnumerator2();
    int iii = 0;
    while (messageEnumerator.MoveNext())
    {
       iii++;
    }

    myPrivateQueuesCount.Add(iii);
    return;//i;
}

I get the error.

System.Messaging.MessageQueueException (0x80004005): Sharing violation resulted from queue being open already for exclusive receive.

How can I go about reading the queue to do a count without trying to get exclusive access on it. I just need to count it.

Thank you

Regards

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Dark_seth
  • 3
  • 1
  • Have you tried Peek? But to achieve what you want, you may be better using the queue name and Performance counters: https://stackoverflow.com/a/13139144/474702 – reckface Aug 15 '17 at 07:26
  • Yes. I just came back here to update and post the answer. I did use performance Counters. So I get the queue name. Trim it so it matches performance counter and it is working. – Dark_seth Aug 15 '17 at 09:06

1 Answers1

0

I used performance Counter to read the queue. Working like a dream now!

Added the catch exception. This is for when the queue is blank. I write a 0. Performance counter gives error on blank queue.

       public void Count(String path)
    {

            path = path.Remove(0, 21);

        try
       {
            PerformanceCounter queueCounter = new PerformanceCounter(
             "MSMQ Queue",
             "Messages in Queue",
            @path);


            Console.WriteLine("Queue contains {0} messages",
                queueCounter.NextValue().ToString());
            myPrivateQueuesCount.Add((int)queueCounter.NextValue());

        }
        catch (Exception exc)
        {
            myPrivateQueuesCount.Add(0);
        }

        return;
    }
Dark_seth
  • 3
  • 1