MessageQueue.Close Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Zwalnia wszystkie zasoby przydzielone przez element MessageQueue.
public:
void Close();
public void Close();
member this.Close : unit -> unit
Public Sub Close ()
Przykłady
Poniższy przykład kodu zamyka kolejkę kolejkowania komunikatów.
#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
Uwagi
Close zwalnia wszystkie zasoby skojarzone z elementem MessageQueue, w tym współużytkowanych zasobów, jeśli jest to konieczne. System ponownie uzyskuje te zasoby automatycznie, jeśli są one nadal dostępne, na przykład podczas wywoływania Send(Object) metody, jak w poniższym kodzie języka C#.
myMessageQueue.Send("Text 1.");
myMessageQueue.Close();
myMessageQueue.Send("Text 2."); //Resources are re-acquired.
Po wywołaniu Closemetody wszystkie MessageQueue właściwości, które bezpośrednio uzyskują dostęp do kolejki kolejki kolejkowania komunikatów, zostaną wyczyszczone. Wszystkie Pathelementy , DefaultPropertiesToSend, Formatteri MessageReadPropertyFilter pozostają tak, jak były.
Close nie zawsze zwalnia uchwyty odczytu i zapisu do kolejki, ponieważ mogą być udostępniane. Możesz wykonać dowolną z poniższych czynności, aby upewnić się, że Close zwalnia do kolejki dojścia do odczytu i zapisu:
Utwórz element MessageQueue z wyłącznym dostępem. W tym celu wywołaj MessageQueue(String, Boolean) konstruktor lub MessageQueue(String, Boolean, Boolean) i ustaw
sharedModeDenyReceiveparametr natrue.Utwórz obiekt MessageQueue z wyłączoną buforowaniem połączenia. W tym celu wywołaj MessageQueue(String, Boolean, Boolean) konstruktor i ustaw
enableConnectionCacheparametr nafalse.Wyłącz buforowanie połączeń. W tym celu ustaw EnableConnectionCache właściwość na
false.
Przed usunięciem kolejki na serwerze kolejkowania komunikatów należy wywołać Close kolejkę. W przeciwnym razie komunikaty wysyłane do kolejki mogą zgłaszać wyjątki lub pojawiać się w kolejce utraconych komunikatów.
W poniższej tabeli przedstawiono, czy ta metoda jest dostępna w różnych trybach grupy roboczej.
| Tryb grupy roboczej | Dostępny |
|---|---|
| Komputer lokalny | Yes |
| Nazwa komputera lokalnego i bezpośredniego formatu | Yes |
| Komputer zdalny | Yes |
| Nazwa komputera zdalnego i bezpośredniego formatu | Yes |