C#中使用Windows消息队列服务(MSMQ)简单示例

简介:
using  System;
InBlock.gif using  System.Collections.Generic;
InBlock.gif using  System.ComponentModel;
InBlock.gif using  System.Data;
InBlock.gif using  System.Drawing;
InBlock.gif using  System.Text;
InBlock.gif using  System.Windows.Forms;
InBlock.gif using  System.Messaging;
InBlock.gif
namespace  WindowsApplication1
InBlock.gif {
InBlock.gif          public  partial  class  Form1 : Form
InBlock.gif         {
InBlock.gif                  public  Form1()
InBlock.gif                 {
InBlock.gif                         InitializeComponent();
InBlock.gif                 }
InBlock.gif
                 string  QueuePath =  ".\\private$\\test" ;
InBlock.gif                 IMessageFormatter formatter =  new  System.Messaging.BinaryMessageFormatter();
InBlock.gif
                 private   void  button1_Click( object  sender, EventArgs e)
InBlock.gif                 {
InBlock.gif                         CreateMessageQueue(QueuePath);
InBlock.gif                         SendMessage(QueuePath, CreateMessage(richTextBox1.Text, formatter));
InBlock.gif                 }
InBlock.gif
                 private   void  button2_Click( object  sender, EventArgs e)
InBlock.gif                 {
InBlock.gif                         System.Messaging.Message msg = ReceiveMessage(QueuePath);
InBlock.gif                         msg.Formatter = formatter;
InBlock.gif                         richTextBox2.Text = msg.Body.ToString();
InBlock.gif                 }
InBlock.gif
                 private  System.Messaging.Message CreateMessage( string  text, IMessageFormatter formatter)
InBlock.gif                 {
InBlock.gif                         System.Messaging.Message message =  new  System.Messaging.Message();
InBlock.gif                         message.Body = text;
InBlock.gif                         message.Formatter = formatter;
InBlock.gif                          return  message;
InBlock.gif                 }
InBlock.gif
                 private   void  CreateMessageQueue( string  queuePath)
InBlock.gif                 {
InBlock.gif                          if  (!MessageQueue.Exists(queuePath))
InBlock.gif                         {
InBlock.gif                                 MessageQueue queue = MessageQueue.Create(queuePath);
InBlock.gif                                 queue.SetPermissions( "Administrators" , MessageQueueAccessRights.FullControl);
InBlock.gif                                 queue.Label = queuePath;
InBlock.gif                         }
InBlock.gif                 }
InBlock.gif
                 private   bool  SendMessage( string  queuePath, System.Messaging.Message msg)
InBlock.gif                 {
InBlock.gif                          if  (!MessageQueue.Exists(queuePath))
InBlock.gif                         {
InBlock.gif                                  return   false ;
InBlock.gif                         }
InBlock.gif
                        MessageQueue queue =  new  System.Messaging.MessageQueue(queuePath);
InBlock.gif                         queue.Send(msg);
InBlock.gif                          return   true ;
InBlock.gif                 }
InBlock.gif
                 private  System.Messaging.Message ReceiveMessage( string  queuePath)
InBlock.gif                 {
InBlock.gif                          if  (!MessageQueue.Exists(queuePath))
InBlock.gif                         {
InBlock.gif                                  return   null ;
InBlock.gif                         }
InBlock.gif
                        MessageQueue queue =  new  MessageQueue(queuePath);
InBlock.gif                         System.Messaging.Message message = queue.Receive();
InBlock.gif                          return  message;
InBlock.gif                 }
InBlock.gif         }
InBlock.gif }










本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/184323,如需转载请自行联系原作者
目录
相关文章
|
30天前
|
Java 数据库 C#
C#winforms实现windows窗体人脸识别
C#winforms实现windows窗体人脸识别
29 0
|
1月前
|
Linux Shell Windows
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
|
1月前
|
存储 安全 数据安全/隐私保护
Windows部署WebDAV服务并映射到本地盘符实现公网访问本地存储文件
Windows部署WebDAV服务并映射到本地盘符实现公网访问本地存储文件
252 0
|
2月前
|
自然语言处理 C# Windows
C#开源免费的Windows右键菜单管理工具
C#开源免费的Windows右键菜单管理工具
|
2月前
|
Java Unix 应用服务中间件
使用java service wrapper把windows flume做成服务
使用java service wrapper把windows flume做成服务
|
2月前
|
Windows
修改Windows服务的配置
修改Windows服务的配置
|
22天前
|
Shell Windows
Windows服务器 开机自启动服务
Windows服务器 开机自启动服务
13 0
|
1月前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
|
2月前
|
安全 数据安全/隐私保护 Windows
Windows系统搭建VisualSVN实现公网访问本地服务
Windows系统搭建VisualSVN实现公网访问本地服务
48 0
|
3月前
|
定位技术 C# 图形学
Unity和C#游戏编程入门:创建迷宫小球游戏示例
Unity和C#游戏编程入门:创建迷宫小球游戏示例
71 2