C# MyNewQueue 消息队列
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 消息队列的更多相关文章
- 消息队列——RabbitMQ学习笔记
消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...
- 消息队列 Kafka 的基本知识及 .NET Core 客户端
前言 最新项目中要用到消息队列来做消息的传输,之所以选着 Kafka 是因为要配合其他 java 项目中,所以就对 Kafka 了解了一下,也算是做个笔记吧. 本篇不谈论 Kafka 和其他的一些消息 ...
- .net 分布式架构之业务消息队列
开源QQ群: .net 开源基础服务 238543768 开源地址: http://git.oschina.net/chejiangyi/Dyd.BusinessMQ ## 业务消息队列 ##业务消 ...
- 【原创经验分享】WCF之消息队列
最近都在鼓捣这个WCF,因为看到说WCF比WebService功能要强大许多,另外也看了一些公司的招聘信息,貌似一些中.高级的程序员招聘,都有提及到WCF这一块,所以,自己也关心关心一下,虽然目前工作 ...
- Java消息队列--ActiveMq 实战
1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...
- Java消息队列--JMS概述
1.什么是JMS JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送 ...
- 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)
Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...
- Netty构建分布式消息队列(AvatarMQ)设计指南之架构篇
目前业界流行的分布式消息队列系统(或者可以叫做消息中间件)种类繁多,比如,基于Erlang的RabbitMQ.基于Java的ActiveMQ/Apache Kafka.基于C/C++的ZeroMQ等等 ...
- Netty构建分布式消息队列实现原理浅析
在本人的上一篇博客文章:Netty构建分布式消息队列(AvatarMQ)设计指南之架构篇 中,重点向大家介绍了AvatarMQ主要构成模块以及目前存在的优缺点.最后以一个生产者.消费者传递消息的例子, ...
随机推荐
- wampserver 绑定域名(wampserver 本地域名测试配置)
一.tomact 配置虚拟主机 1.打开Apache菜单下“httpd.conf”文件: 找到“# Include conf/extra/httpd-vhosts.conf” , 把这句前面的#号去掉 ...
- Python中初始化的问题以及注释问题
#coding=utf-8 # __author__ = 'minmin' from Tkinter import * #创建一个基于Frame的Application类 class Applicat ...
- hdu 4735Little Wish~ lyrical step~ 重复覆盖
题目链接 给出一棵树, 树上点的值为0或1, 可以交换树上两个点的权值, 给出一个距离m, 所有的0距离最近的1的距离不能超过m, 求最少的交换次数. 首先对于每一个点u,所有离u的距离不超过m的点v ...
- VirtualBox 更改主机和虚拟机之间的鼠标切换热键
VirtualBox 主机和虚拟机之间的鼠标切换热键 是 Right Ctrl.一般用惯VM Ware的都不太习惯,那么怎么改变VirtualBox鼠标切换热键呢? 1.点击菜单栏的 管理--&g ...
- github/python/ show me the code 25题(二)
第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示: { "1":["张三",150,120,100], &q ...
- mysql-connector-c 安装
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr/local make make install
- sql server 修改表自增列的值
Create PROCEDURE [dbo].[SP_UpdateIdentityId] ( ) , @beforeId INT , @afterId INT ) AS BEGIN IF @befor ...
- 在Linux下使用iconv转换字符串编码
在Linux下写C程序,尤其是网络通信程序时经常遇到编码转换的问题,这里要用到iconv函数库. iconv函数库有以下三个函数 123456 #include <iconv.h>icon ...
- 如何使用picasso 对Android图片下载缓存
相比较其他,picasso的图片缓存更加简单一些,他只需要一行代码就可以表述:导入相关jar包 Picasso.with(context).load("图片路径").into(Im ...
- nodejs - json序列化&反序列化示例
// demo-json.js var obj = { "name": "LiLi", "age": 22, "sex" ...