MessageQueue.Close-Methode
Gibt alle von der MessageQueue reservierten Ressourcen frei.
Namespace: System.Messaging
Assembly: System.Messaging (in system.messaging.dll)
Syntax
'Declaration
Public Sub Close
'Usage
Dim instance As MessageQueue
instance.Close
public void Close ()
public:
void Close ()
public void Close ()
public function Close ()
Hinweise
Close gibt die einer MessageQueue zugeordneten Ressourcen frei (einschließlich eventueller gemeinsam verwendeter Ressourcen). Wenn die Ressourcen noch verfügbar sind, werden sie vom System automatisch wieder zugewiesen, beispielsweise beim Aufruf der Send(Object)-Methode im folgenden C#-Code.
myMessageQueue.Send("Text 1.");
myMessageQueue.Close();
myMessageQueue.Send("Text 2."); //Resources are re-acquired.
Beim Aufruf von Close werden alle MessageQueue-Eigenschaften gelöscht, die direkt auf die Message Queuing-Warteschlange zugreifen. Die Werte von Path, DefaultPropertiesToSend, Formatter und MessageReadPropertyFilter bleiben erhalten.
Close gibt der Warteschlange die Lese- und Schreibhandles nicht immer frei, da sie möglicherweise auch von anderen Anwendungen verwendet werden. Mit einem der folgenden Schritte können Sie sicherstellen, dass Close einer Warteschlange die Lese- und Schreibhandles freigibt:
Erstellen Sie die MessageQueue mit exklusivem Zugriff. Rufen Sie hierfür den MessageQueue(String,Boolean)-Konstruktor oder den MessageQueue(String,Boolean,Boolean)-Konstruktor auf, und legen Sie den sharedModeDenyReceive-Parameter auf true fest.
Deaktivieren Sie beim Erstellen der MessageQueue das Verbindungscaching. Rufen Sie hierfür den MessageQueue(String,Boolean,Boolean)-Konstruktor auf, und legen Sie den enableConnectionCache-Parameter auf false fest.
Deaktivieren Sie Verbindungscaching. Legen Sie hierfür die EnableConnectionCache-Eigenschaft auf falsefest.
Rufen Sie Close für eine Warteschlange auf, bevor Sie diese auf dem Message Queuing-Server löschen. Andernfalls können an die Warteschlange gesendete Meldungen Ausnahmen auslösen oder in der Dead Letter-Warteschlange erscheinen.
Der folgenden Tabelle können Sie entnehmen, ob diese Methode in verschiedenen Arbeitsgruppenmodi verfügbar ist.
Arbeitsgruppenmodus |
Verfügbar |
---|---|
Lokaler Computer |
Ja |
Lokaler Computer + direkter Formatname |
Ja |
Remotecomputer |
Ja |
Lokaler Computer + direkter Formatname |
Ja |
Beispiel
Im folgenden Codebeispiel wird eine Message Queuing-Warteschlange geschlossen.
Imports System
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 '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 reacquire 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
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;
}
}
}
#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;
}
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
.NET Framework-Sicherheit
- Volle Vertrauenswürdigkeit für den unmittelbaren Aufrufer. Dieser Member kann von nur teilweise vertrauenswürdigem Code nicht verwendet werden. Weitere Informationen finden Sie unter .
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0
Siehe auch
Referenz
MessageQueue-Klasse
MessageQueue-Member
System.Messaging-Namespace