Export (0) Print
Expand All

MessageQueue.Close Method

Frees all resources allocated by the MessageQueue.

Namespace: System.Messaging
Assembly: System.Messaging (in system.messaging.dll)

public void Close ()
public void Close ()
public function Close ()

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(Object) method, as in the following C# code.

 myMessageQueue.Send("Text 1.");
 myMessageQueue.Close();
 myMessageQueue.Send("Text 2."); //Resources are re-acquired.

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.

Close does not always free the read and write handles to a queue, because they might be shared. You can take any of the following steps to ensure that Close frees the read and write handles to a queue:

  • Create the MessageQueue with exclusive access. To do so, call the MessageQueue(String,Boolean) or MessageQueue(String,Boolean,Boolean) constructor, and set the sharedModeDenyReceive parameter to true.

  • Create the MessageQueue with connection caching disabled. To do so, call the MessageQueue(String,Boolean,Boolean) constructor and set the enableConnectionCache parameter to false.

  • Disable connection caching. To do so, set the EnableConnectionCache property to false.

You should call Close for a queue before you delete the 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 and direct format name

Yes

Remote computer

Yes

Remote computer and direct format name

Yes

The following code example closes a Message Queuing queue.

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 reacquire 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;
		}
	}
}

package MyProject;

import System.*;
import System.Messaging.*;

/// <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(String[] args)
    {
        // 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;
    } //main

    //**************************************************
    // 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;
    } //SendMessage

    //**************************************************
    // 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.set_Formatter(new XmlMessageFormatter(new Type[]
            { String.class.ToType() }));

        try {
            // Receive and format the message. 
            Message myMessage1 = myQueue.Receive();
            Message myMessage2 = myQueue.Receive();
        }
        catch (MessageQueueException exp) {
            // Handle sources of any MessageQueueException.
        }
        // Catch other exceptions as necessary.
        finally {
            // Free resources.
            myQueue.Close();
        }
        return;
    } //ReceiveMessage
} //MyNewQueue

  • Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see .

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft