本文整理汇总了C#中System.Messaging.MessageQueue类的典型用法代码示例。如果您正苦于以下问题:C# MessageQueue类的具体用法?C# MessageQueue怎么用?C# MessageQueue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MessageQueue类属于System.Messaging命名空间,在下文中一共展示了MessageQueue类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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 demonstrates several ways to set
// a queue's path.
//**************************************************
public static void Main()
{
// Create a new instance of the class.
MyNewQueue myNewQueue = new MyNewQueue();
myNewQueue.SendPublic();
myNewQueue.SendPrivate();
myNewQueue.SendByLabel();
myNewQueue.SendByFormatName();
myNewQueue.MonitorComputerJournal();
myNewQueue.MonitorQueueJournal();
myNewQueue.MonitorDeadLetter();
myNewQueue.MonitorTransactionalDeadLetter();
return;
}
// References public queues.
public void SendPublic()
{
MessageQueue myQueue = new MessageQueue(".\\myQueue");
myQueue.Send("Public queue by path name.");
return;
}
// References private queues.
public void SendPrivate()
{
MessageQueue myQueue = new
MessageQueue(".\\Private$\\myQueue");
myQueue.Send("Private queue by path name.");
return;
}
// References queues by label.
public void SendByLabel()
{
MessageQueue myQueue = new MessageQueue("Label:TheLabel");
myQueue.Send("Queue by label.");
return;
}
// References queues by format name.
public void SendByFormatName()
{
MessageQueue myQueue = new
MessageQueue("FormatName:Public=5A5F7535-AE9A-41d4" +
"-935C-845C2AFF7112");
myQueue.Send("Queue by format name.");
return;
}
// References computer journal queues.
public void MonitorComputerJournal()
{
MessageQueue computerJournal = new
MessageQueue(".\\Journal$");
while(true)
{
Message journalMessage = computerJournal.Receive();
// Process the journal message.
}
}
// References queue journal queues.
public void MonitorQueueJournal()
{
MessageQueue queueJournal = new
MessageQueue(".\\myQueue\\Journal$");
while(true)
{
Message journalMessage = queueJournal.Receive();
// Process the journal message.
}
}
// References dead-letter queues.
public void MonitorDeadLetter()
{
MessageQueue deadLetter = new
MessageQueue(".\\DeadLetter$");
while(true)
{
Message deadMessage = deadLetter.Receive();
// Process the dead-letter message.
}
}
// References transactional dead-letter queues.
public void MonitorTransactionalDeadLetter()
{
MessageQueue TxDeadLetter = new
MessageQueue(".\\XactDeadLetter$");
while(true)
{
Message txDeadLetter = TxDeadLetter.Receive();
// Process the transactional dead-letter message.
}
}
}
}
示例2: Main
//引入命名空间
using System;
using System.Messaging;
namespace MyProject
{
// This class represents an object the following example
// sends to a queue and receives from a queue.
public class Order
{
public int orderId;
public DateTime orderTime;
};
/// <summary>
/// Provides a container class for the example.
/// </summary>
public class MyNewQueue
{
//**************************************************
// Provides an entry point into the application.
//
// This example sends and receives a message from
// a queue.
//**************************************************
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 an Order to a queue.
//**************************************************
public void SendMessage()
{
// Create a new order and set values.
Order sentOrder = new Order();
sentOrder.orderId = 3;
sentOrder.orderTime = DateTime.Now;
// Connect to a queue on the local computer.
MessageQueue myQueue = new MessageQueue(".\\myQueue");
// Send the Order to the queue.
myQueue.Send(sentOrder);
return;
}
//**************************************************
// Receives a message containing an Order.
//**************************************************
public void ReceiveMessage()
{
// Connect to the a queue 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(MyProject.Order)});
try
{
// Receive and format the message.
Message myMessage = myQueue.Receive();
Order myOrder = (Order)myMessage.Body;
// Display message information.
Console.WriteLine("Order ID: " +
myOrder.orderId.ToString());
Console.WriteLine("Sent: " +
myOrder.orderTime.ToString());
}
catch (MessageQueueException)
{
// Handle Message Queuing exceptions.
}
// Handle invalid serialization format.
catch (InvalidOperationException e)
{
Console.WriteLine(e.Message);
}
// Catch other exceptions as necessary.
return;
}
}
}
示例3: new MessageQueue
//引入命名空间
using System;
using System.Messaging;
class MainClass
{
[STAThread]
static void Main(string[] args)
{
MessageQueue txq1 = new MessageQueue( @".\Private$\txq1" );
for ( int i =1; i <= 20; i++ )
{
Message msgOut = new Message();
msgOut.Body = "Message " + i;
txq1.Send( msgOut, MessageQueueTransactionType.Single );
}
}
}