Search code examples
c#msmqdispose

Calling Close() or Dispose() on MessageQueue does nothing


I am having an issue with calling Close() or Dispose() on a MessageQueue. I got an application that is supposed to be able to manage these message queues. Open and Close them.

But when I call Close() or Dispose() nothing happens. The queue still exists on the machine. I use the MessageQueue.GetPrivateQueuesByMachine() method to get an array of MessageQueue's and it's still in there after calling Close() or Dispose(). After some reading around it seems that Dipose() should be the one I use but still.

This is the code I use to try and close the queue

public String CloseQueue(String path)
{
    if (GetQueueArray().Length == 0)
    {
        return "The Queue Array is Empty!";
    }

    foreach (MessageQueue m in GetQueueArray())
    {
        if (m.Path.Equals(path))
        {
            try
            {
                path = m.QueueName;
                m.Close();
                break;
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
    }
    return "Successfully Closed Queue at path: " + path;
}

The methodGetQueueArray() is defined as so (in the same class):

public MessageQueue[] GetQueueArray()
{
    return MessageQueue.GetPrivateQueuesByMachine(machinename);
}

I do this just out of convenience in my GUI Application to call that method when needed.

How do I actually close or dispose of the queues?


Solution

  • Close or Dispose only work for your instance of the message queue C# object. They do not affect the actual message queue. The same way that disposing a database connection will not automatically remove the whole database from the server.

    You need to use the static Delete method of MessageQueue.