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 將讀取和寫入句柄釋放至佇列:
Create 具有獨佔存取權的 MessageQueue 。 若要這樣做,請呼叫 MessageQueue(String, Boolean) 或 MessageQueue(String, Boolean, Boolean) 建構函式,並將 參數設定
sharedModeDenyReceive
為true
。MessageQueue Create 停用連線快取的 。 若要這樣做,請呼叫建構函式, MessageQueue(String, Boolean, Boolean) 並將 參數設定
enableConnectionCache
為false
。停用連線快取。 若要這樣做,請將 EnableConnectionCache 屬性設定為
false
。
您應該先呼叫 Close 佇列,再刪除消息佇列伺服器上的佇列。 否則,傳送至佇列的訊息可能會擲回例外狀況,或出現在寄不出的信件佇列中。
下表顯示此方法是否可在各種工作組模式中使用。
工作組模式 | 可用 |
---|---|
本機電腦 | 是 |
本機計算機和直接格式名稱 | 是 |
遠端電腦 | 是 |
遠端電腦和直接格式名稱 | 是 |