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. R与数据分析旧笔记(十八完结) 因子分析

    因子分析 因子分析 降维的一种方法,是主成分分析的推广和发展 是用于分析隐藏在表面现象背后的因子作用的统计模型.试图用最少的个数的不可测的公共因子的线性函数与特殊因子之和来描述原来观测的每一分量 因子 ...

  2. 转载:js 创建对象、属性、方法

    1,自定义对象. 根据JS的对象扩展机制,用户可以自定义JS对象,这与Java语言有类似的地方. 与自定义对象相对应的是JS标准对象,例如Date.Array.Math等等. 2,原型(prototy ...

  3. yii2.0 从控制器到视图的输出

    在controllers/SiteController.php文件中,添加 public function actionSay($message = 'Hello') { return $this-& ...

  4. “use strict”对js的影响

    一:全局变量显示声明 在正常模式下,如果一个变量没有声明就赋值,默认是全局变量,严格模式禁止用这种方法.全局变量必须显示声明. ; i++) { function f2() { } // 语法错误 } ...

  5. sqlplus命令手册

    show errorshow allshow usersqlplus show和set命令是两条用于维护SQLPlus系统变量的命令 : SQL> show all --查看所有系统变量值 SQ ...

  6. 富文本编辑器ckeditor继承

    新建一个web项目ckfinder,导入lib包 加入java包,编码格式UTF-8 在WebRoot下添加ckedtior以及ckfinder两个文件夹,将config.xml拷入WEB-INF中 ...

  7. PL/SQL database character set(AL32UTF8) and Client character set(ZHS16GBK) are different

    启动PL/SQL Developer 报字符编码不一致错误 Database character set (AL32UTF8) and Client character set (ZHS16GBK) ...

  8. MySQL float 与decimal 各中的区别。

    想一个问题: 1/3+1/3+1/3=1.0 0.3+0.3+0.3 =0.9 想一想在小数的世界里要什么表示1/3呢!它的办法就是取一个与1/3十分接近的小数来代替:如上面例子中的0.3来代替1/3 ...

  9. HDU 4366 Successor(树链剖分+zkw线段树+扫描线)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4366 [题目大意] 有一个公司,每个员工都有一个上司,所有的人呈树状关系,现在给出每个人的忠诚值和 ...

  10. tarjan算法大意

    Tarjan算法 (以发现者Robert Tarjan命名)是一个在图中寻找强连通分量的算法.算法的基本思想为:任选一结点开始进行深度优先搜索dfs(若深度优先搜索结束后仍有未访问的结点,则再从中任选 ...