MessageQueue.Close 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
释放 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关联的所有资源,包括共享资源(如果适用)。 如果这些资源仍然可用(例如调用 Send(Object) 方法时),系统会自动重新获取这些资源,如以下 C# 代码所示。
myMessageQueue.Send("Text 1.");
myMessageQueue.Close();
myMessageQueue.Send("Text 2."); //Resources are re-acquired.
调用 Close时,将清除直接访问消息队列的所有 MessageQueue 属性。 Path、 DefaultPropertiesToSend、 Formatter和 MessageReadPropertyFilter 都保持不变。
Close 不会始终将读取和写入句柄释放到队列,因为它们可能是共享的。 可以执行以下任何步骤来确保 Close 将读取和写入句柄释放到队列:
CreateMessageQueue具有独占访问权限的 。 为此,请调用 MessageQueue(String, Boolean) 或 构造函数,并将 参数设置为
sharedModeDenyReceive
true
。MessageQueue(String, Boolean, Boolean)在禁用连接缓存的情况下Create MessageQueue 。 为此,请调用 构造函数并将 MessageQueue(String, Boolean, Boolean) 参数设置为
enableConnectionCache
false
。禁用连接缓存。 为此,请将 属性 EnableConnectionCache 设置为
false
。
删除消息队列服务器上的队列之前,应调用 Close 队列。 否则,发送到队列的消息可能会引发异常或出现在死信队列中。
下表显示了此方法在各种工作组模式下是否可用。
工作组模式 | 可用 |
---|---|
本地计算机 | 是 |
本地计算机和直接格式名称 | 是 |
远程计算机 | 是 |
远程计算机和直接格式名称 | 是 |