MessageQueue.Close Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Uvolní všechny prostředky přidělené MessageQueuenástrojem .
public:
void Close();
public void Close();
member this.Close : unit -> unit
Public Sub Close ()
Příklady
Následující příklad kódu zavře frontu služby Řízení front zpráv.
#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
Poznámky
Close uvolní všechny prostředky přidružené k prostředku MessageQueue, včetně sdílených prostředků, pokud je to vhodné. Systém tyto prostředky znovu získá automaticky, pokud jsou stále dostupné, například při volání Send(Object) metody, jako v následujícím kódu jazyka C#.
myMessageQueue.Send("Text 1.");
myMessageQueue.Close();
myMessageQueue.Send("Text 2."); //Resources are re-acquired.
Při volání Closese vymažou všechny MessageQueue vlastnosti, které přímo přistupují ke frontě služby Řízení front zpráv. , PathDefaultPropertiesToSendFormattera MessageReadPropertyFilter všichni zůstávají tak, jak byli.
Close ne vždy uvolní popisovače pro čtení a zápis do fronty, protože se můžou sdílet. Můžete provést některý z následujících kroků, abyste zajistili, že Close uvolní obslužné rutiny pro čtení a zápis do fronty:
Vytvořte s MessageQueue výhradním přístupem. Uděláte to tak, že zavoláte MessageQueue(String, Boolean) nebo MessageQueue(String, Boolean, Boolean) konstruktor a nastavíte
sharedModeDenyReceiveparametr natruehodnotu .Vytvoření se zakázaným ukládáním MessageQueue do mezipaměti připojení Uděláte to tak, že zavoláte MessageQueue(String, Boolean, Boolean) konstruktor a nastavíte
enableConnectionCacheparametr nafalse.Zakažte ukládání připojení do mezipaměti. Uděláte to tak, že EnableConnectionCache vlastnost nastavíte na
falsehodnotu .
Před odstraněním fronty na serveru služby Řízení front zpráv byste měli volat Close frontu. V opačném případě můžou zprávy odeslané do fronty vyvolat výjimky nebo se zobrazit ve frontě nedoručených zpráv.
Následující tabulka ukazuje, zda je tato metoda k dispozici v různých režimech pracovní skupiny.
| Režim pracovní skupiny | K dispozici |
|---|---|
| Místní počítač | Ano |
| Název místního počítače a přímého formátu | Ano |
| Vzdálený počítač | Ano |
| Název vzdáleného počítače a přímého formátu | Ano |