Export (0) Print
Expand All

MessageQueue.Close Method

Frees all resources allocated by the MessageQueue.

[Visual Basic]
Public Sub Close()
[C#]
public void Close();
[C++]
public: void Close();
[JScript]
public function Close();

Remarks

Close frees all resources associated with a MessageQueue, including shared resources if appropriate. The system re-acquires these resources automatically if they are still available, for example when you call the Send method, as in the following C# code.

myMessageQueue.Send("Text 1.");
myMessageQueue.Close();
myMessageQueue.Send("Text 2."); //Resources are reaquired.

When you call Close, all MessageQueue properties that directly access the Message Queuing queue are cleared out. The Path, DefaultPropertiesToSend, Formatter, and MessageReadPropertyFilter all remain as they were.

You should call Close for a queue before you delete queue on the Message Queuing server. Otherwise, messages sent to the queue could throw exceptions or appear in the dead-letter queue.

The following table shows whether this method is available in various Workgroup modes.

Workgroup Mode Available
Local computer Yes
Local computer + direct format name Yes
Remote computer Yes
Remote computer + direct format name Yes

Example

[Visual Basic, C#, C++] The following example closes a Message Queuing queue.

[Visual Basic] 
Imports System
Imports System.Messaging

Namespace MyProject

    '/ <summary>
    '/ Provides a container class for the example.
    '/ </summary>
    Public Class MyNewQueue


        '**************************************************
        ' Provides an entry point into the application.
        '     
        ' This example closes a queue and frees its 
        ' resources.
        '**************************************************

        Public Shared Sub Main()

            ' Create a new instance of the class.
            Dim myNewQueue As New MyNewQueue()

            ' Send a message to a queue.
            myNewQueue.SendMessage()

            ' Receive a message from a queue.
            myNewQueue.ReceiveMessage()

            Return

        End Sub 'Main


        '**************************************************
        ' Sends a message to a queue.
        '**************************************************

        Public Sub SendMessage()

            ' Connect to a queue on the local computer.
            Dim myQueue As New MessageQueue(".\myQueue")

            ' Send a message to the queue.
            myQueue.Send("My message data1.")

            ' Explicitly release resources.
            myQueue.Close()

            ' Attempt to reaquire resources.
            myQueue.Send("My message data2.")

            Return

        End Sub 'SendMessage


        '**************************************************
        ' Receives a message from a queue.
        '**************************************************

        Public Sub ReceiveMessage()

            ' Connect to the a on the local computer.
            Dim myQueue As New MessageQueue(".\myQueue")

            ' Set the formatter to indicate the body contains an 
            ' Order.
            myQueue.Formatter = New XmlMessageFormatter(New Type() _
                {GetType([String])})

            Try
                ' Receive and format the message. 
                Dim myMessage1 As Message = myQueue.Receive()
                Dim myMessage2 As Message = myQueue.Receive()

            Catch
                ' Handle sources of any MessageQueueException.

                ' Catch other exceptions as necessary.

            Finally

                ' Free resources.
                myQueue.Close()

            End Try

            Return

        End Sub 'ReceiveMessage

    End Class 'MyNewQueue
End Namespace 'MyProject

[C#] 
using System;
using System.Messaging;

namespace MyProject
{

    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {

        //**************************************************
        // Provides an entry point into the application.
        //     
        // This example closes a queue and frees its 
        // resources.
        //**************************************************

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            // Send a message to a queue.
            myNewQueue.SendMessage();

            // Receive a message from a queue.
            myNewQueue.ReceiveMessage();

            return;
        }


        //**************************************************
        // Sends a message to a queue.
        //**************************************************
        
        public void SendMessage()
        {
            // Connect to a queue on the local computer.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");

            // Send a message to the queue.
            myQueue.Send("My message data1.");
            
            // Explicitly release resources.
            myQueue.Close();

            // Attempt to reaquire resources.
            myQueue.Send("My message data2.");

            return;
        }


        //**************************************************
        // Receives a message from a queue.
        //**************************************************
        
        public  void ReceiveMessage()
        {
            // Connect to the a on the local computer.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");

            // Set the formatter to indicate body contains an Order.
            myQueue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(String)});
            
            try
            {
                // Receive and format the message. 
                Message myMessage1 = myQueue.Receive();
                Message myMessage2 = myQueue.Receive();
            }
            
            catch (MessageQueueException)
            {
                // Handle sources of any MessageQueueException.
            }

            // Catch other exceptions as necessary.

            finally
            {
                // Free resources.
                myQueue.Close();
            }

            return;
        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;

__gc class MyNewQueue 
{
public:
    // Sends a message to a queue.
    void SendMessage() 
    {
        // Connect to a queue on the local computer.
        MessageQueue* myQueue = new MessageQueue(S".\\myQueue");

        // Send a message to the queue.
        myQueue->Send(S"My message data1.");

        // Explicitly release resources.
        myQueue->Close();

        // Attempt to reaquire resources.
        myQueue->Send(S"My message data2.");

        return;
    }

    // Receives a message from a queue.
    void ReceiveMessage() 
    {
        // Connect to the a on the local computer.
        MessageQueue* myQueue = new MessageQueue(S".\\myQueue");

        // Set the formatter to indicate body contains an Order.
        Type* p __gc[] = new Type* __gc[1];
        p[0] = __typeof(String);
        myQueue->Formatter = new XmlMessageFormatter( p );

        try 
        {
            // Receive and format the message. 
            Message* myMessage1 = myQueue->Receive();
            Message* myMessage2 = myQueue->Receive();
        }
        catch (MessageQueueException*) 
        {
            // Handle sources of any MessageQueueException.
        }

        // Catch other exceptions as necessary.
        __finally 
        {
            // Free resources.
            myQueue->Close();
        }

        return;
    }
};

// Provides an entry point into the application.
// This example closes a queue and frees its 
// resources.
int main() 
{
    // Create a new instance of the class.
    MyNewQueue* myNewQueue = new MyNewQueue();

    // Send a message to a queue.
    myNewQueue->SendMessage();

    // Receive a message from a queue.
    myNewQueue->ReceiveMessage();

    return 0;
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

MessageQueue Class | MessageQueue Members | System.Messaging Namespace | Delete

Show:
© 2014 Microsoft