MessageQueue.Close メソッド

定義

MessageQueue で割り当てられたすべてのリソースを解放します。

public:
 void Close();
public void Close ();
member this.Close : unit -> unit
Public Sub Close ()

次のコード例では、メッセージ キューキューを閉じます。

#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;
ref class MyNewQueue
{
public:

   // Sends a message to a queue.
   void SendMessage()
   {
      // Connect to a queue on the local computer.
      MessageQueue^ myQueue = gcnew 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.
   void ReceiveMessage()
   {
      // Connect to the a on the local computer.
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );

      // Set the formatter to indicate body contains an Order.
      array<Type^>^p = gcnew array<Type^>(1);
      p[ 0 ] = String::typeid;
      myQueue->Formatter = gcnew XmlMessageFormatter( p );
      try
      {
         // Receive and format the message. 
         Message^ myMessage1 = myQueue->Receive();
         Message^ myMessage2 = myQueue->Receive();
      }
      catch ( MessageQueueException^ ) 
      {
         // Handle sources of any MessageQueueException.
      }
      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 = gcnew MyNewQueue;

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

   // Receive a message from a queue.
   myNewQueue->ReceiveMessage();
   return 0;
}
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;
        }
    }
}
Imports System.Messaging

  
'Provides a container class for the example.

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



        ' 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 reacquire resources.
            myQueue.Send("My message data2.")

            Return

        End Sub



        ' 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

End Class

注釈

Close は、 に関連付けられているすべてのリソース (必要に応じて共有リソースを MessageQueue含む) を解放します。 次の C# コードのように、 メソッドを呼び出 Send(Object) す場合など、これらのリソースがまだ使用可能な場合は、これらのリソースが自動的に再取得されます。

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

を呼び出Closeすと、メッセージ キューキューに直接アクセスするすべてのMessageQueueプロパティがクリアされます。、PathDefaultPropertiesToSendFormatter、および MessageReadPropertyFilter はすべて、そのまま残ります。

Close は、読み取りハンドルと書き込みハンドルが共有される可能性があるため、常にキューに解放されるとは限りません。 読み取りハンドルと書き込みハンドルをキューに解放 Close するには、次のいずれかの手順を実行します。

メッセージ キュー サーバー上のキューを削除する前に、キューを呼び出 Close す必要があります。 そうしないと、キューに送信されたメッセージが例外をスローしたり、配信不能キューに表示されたりする可能性があります。

次の表は、このメソッドがさまざまなワークグループ モードで使用できるかどうかを示しています。

ワークグループ モード 利用可能
ローカル コンピューター はい
ローカル コンピューターと直接形式の名前 はい
リモート コンピューター はい
リモート コンピューターと直接形式の名前 はい

適用対象