C#
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()); // Send the image to the queue.
myQueue.Send(myMessage);
}
catch(ArgumentException e)
{
Console.WriteLine(e.Message); } return;
} /***************************************************/
// Receives a message containing an image.
/***************************************************/ public void ReceiveMessage()
{ try
{ // 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 BinaryMessageFormatter(); // Receive and format the message.
System.Messaging.Message myMessage = myQueue.Receive();
Bitmap myImage = (Bitmap)myMessage.Body; // This will be saved in the \bin\debug or \bin\retail folder.
myImage.Save("ReceivedImage.bmp",System.Drawing.Imaging.ImageFormat.Bmp); } catch (MessageQueueException)
{
// Handle Message Queuing exceptions.
} // Handle invalid serialization format.
catch (InvalidOperationException e)
{
Console.WriteLine(e.Message);
} catch (IOException e)
{
// Handle file access exceptions.
} // Catch other exceptions as necessary. return;
}
}
}
转载原文:
http://msdn.microsoft.com/zh-cn/beginner/system.messaging.message%28VS.110%29.aspx

C# MyNewQueue 消息队列的更多相关文章

  1. 消息队列——RabbitMQ学习笔记

    消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...

  2. 消息队列 Kafka 的基本知识及 .NET Core 客户端

    前言 最新项目中要用到消息队列来做消息的传输,之所以选着 Kafka 是因为要配合其他 java 项目中,所以就对 Kafka 了解了一下,也算是做个笔记吧. 本篇不谈论 Kafka 和其他的一些消息 ...

  3. .net 分布式架构之业务消息队列

    开源QQ群: .net 开源基础服务  238543768 开源地址: http://git.oschina.net/chejiangyi/Dyd.BusinessMQ ## 业务消息队列 ##业务消 ...

  4. 【原创经验分享】WCF之消息队列

    最近都在鼓捣这个WCF,因为看到说WCF比WebService功能要强大许多,另外也看了一些公司的招聘信息,貌似一些中.高级的程序员招聘,都有提及到WCF这一块,所以,自己也关心关心一下,虽然目前工作 ...

  5. Java消息队列--ActiveMq 实战

    1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...

  6. Java消息队列--JMS概述

    1.什么是JMS JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送 ...

  7. 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)

    Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...

  8. Netty构建分布式消息队列(AvatarMQ)设计指南之架构篇

    目前业界流行的分布式消息队列系统(或者可以叫做消息中间件)种类繁多,比如,基于Erlang的RabbitMQ.基于Java的ActiveMQ/Apache Kafka.基于C/C++的ZeroMQ等等 ...

  9. Netty构建分布式消息队列实现原理浅析

    在本人的上一篇博客文章:Netty构建分布式消息队列(AvatarMQ)设计指南之架构篇 中,重点向大家介绍了AvatarMQ主要构成模块以及目前存在的优缺点.最后以一个生产者.消费者传递消息的例子, ...

随机推荐

  1. 基于KVM建立虚拟机的步骤及总结说明

    1.前言 目前正在涉足云计算IaaS工作,虚拟化是IaaS的重要部分,因此这段时间对各个虚拟机化技术和工具进行研究,研究的目的不仅仅是为了会使用这个工具,而是通过研究了解技术的实现机制和原理,即知其然 ...

  2. RedisTemplate.htm

    http://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/RedisTe ...

  3. 转:一个strcpy的问题(很容易做错)

    下面的执行结果是什么? #include<stdio.h> #include<string.h> void main() { "; "; strcpy(d, ...

  4. AppStore被拒原因及总结

    4.5 - Apps using background location services must provide a reason that clarifies the purpose of th ...

  5. js 定义类对象

    //定义类     //方式一     function A_class(arg1,arg2){         this.arg1=arg1;         this.arg2=arg2;     ...

  6. 有关UIWebView的SSL总结

    在网上找了非常多文章差点儿相同都是一样的,基本上都是关于NSURLConnection的文章. 如今把几个比較好的连接分享给大家http://blog.csdn.net/pingchangtan367 ...

  7. Velocity入门系列

    Velocity介绍 Velocity是一个java模板引擎,通过简洁的语法可以返回动态内容给浏览器使用,本系类是基于velocity官方文档(就是照着翻译,同时对不清楚的地方进行详细讲解),其实技术 ...

  8. ASPxGridview必须设置ShowVerticalScrollBar为true才能动态改变高度。。。

    ASPxGridview必须设置ShowVerticalScrollBar为true才能动态改变高度... 设置 ShowVerticalScrollBar=true ,这时client-side s ...

  9. PKU POJ 1006 Biorhythms (中国剩余定理)

    中国剩余定理 x = ai (mod mi)  ai和mi是一组数,mi两两互质,求x 令Mi = m1*m2*~mk     其中,mi不包含在内. 因为mi两两互质,所以存在x和y, st   M ...

  10. 2015.8.3 Java

    今天继续学习Java  用的是eclipse IDE 这个ide怪怪的,但是有一个很方便的功能 就是通过右键选择source 可以点击Generate Getters and Setters生成属性的 ...