MessageQueue一个例子的使用(代码)

2022-07-15 13:13:20   文档大全网     [ 字体: ] [ 阅读: ]

#文档大全网# 导语】以下是®文档大全网的小编为您整理的《MessageQueue一个例子的使用(代码)》,欢迎阅读!
MessageQueue,例子,代码,使用,一个
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; };

///



/// Provides a container class for the example. ///


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;

本文来源:https://www.wddqxz.cn/77a90a69011ca300a6c39099.html

相关推荐