c# Message Message Queue Count. – Sharing violation resulted from queue being open already for exclusive receive

Home / Uncategorized / c# Message Message Queue Count. – Sharing violation resulted from queue being open already for exclusive receive

Question:
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


Answer:
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;
}
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *