- 作者:zhaozj
- 发表时间:2020-12-23 10:54
- 来源:未知
摘自msdn
using System;using System.Messaging;using System.Drawing;using System.IO;
namespace MyProject{
/// <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();
// Create a queue on the local computer. CreateQueue(".//myQueue"); // Send a message to a queue. myNewQueue.SendMessage();
// Receive a message from a queue. myNewQueue.ReceiveMessage();
return; }
//************************************************** // Creates a new queue. //**************************************************
public static void CreateQueue(string queuePath) { try { if(!MessageQueue.Exists(queuePath)) { MessageQueue.Create(queuePath); } else { Console.WriteLine(queuePath + " already exists."); } } catch (MessageQueueException e) { Console.WriteLine(e.Message); } }
//************************************************** // Sends an image to a queue, using the BinaryMessageFormatter. //************************************************** public void SendMessage() { try{
// Create a new bitmap. // The file must be in the /bin/debug or /bin/retail folder, or // you must give a full path to its location. Image myImage = Bitmap.FromFile("SentImage.bmp");
// Connect to a queue on the local computer. MessageQueue myQueue = new MessageQueue(".//myQueue"); Message myMessage = new Message(myImage, new BinaryMessageFormatter());