本文整理汇总了C#中System.Messaging.MessageQueue.Close方法的典型用法代码示例。如果您正苦于以下问题:C# MessageQueue.Close方法的具体用法?C# MessageQueue.Close怎么用?C# MessageQueue.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Messaging.MessageQueue
的用法示例。
在下文中一共展示了MessageQueue.Close方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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;
}
}
}