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,如需转载请自行联系原作者
目录
相关文章
|
24天前
|
运维 监控 关系型数据库
运维实战:Windows服务挂掉了怎么办,通过Bat脚本实现自动重启
本文介绍了如何使用Bat脚本自动监控并重启Windows服务器上的挂掉服务,例如MySQL,以避免在假期等情况下需要紧急处理问题。首先,创建一个Bat脚本,设定每小时检查一次服务状态,如果服务停止则自动重启。脚本内容包括检查服务是否运行并根据状态执行相应操作。同时,脚本中包含了确保以管理员权限运行的代码。 脚本需设置为ANSI编码以防止乱码。推荐将Bat脚本封装为Windows服务以保证稳定运行,提供了使用NSSM工具、Windows服务程序和开源的Java工具winsw将批处理脚本转化为服务的方法。这些方法可以确保服务在后台可靠运行,即使在服务意外停止时也能自动恢复。
|
7天前
|
开发框架 前端开发 调度
C#基于Quartz.NET实现任务调度并部署Windows服务
C#基于Quartz.NET实现任务调度并部署Windows服务
|
24天前
|
程序员 Windows
程序员必备技能:一键创建windows 服务
该内容是关于在Windows上创建服务的教程。通过一个BAT脚本示例,用户可以将程序设置为开机启动或持续运行的服务。脚本设置了服务名称、显示名和描述,并指定了程序路径和参数。将代码保存为.bat文件,以管理员权限运行,即可创建服务。之后在Windows服务管理中找到并启动该服务。
|
3天前
|
Windows
超详细的纯净windows系统重装示例
超详细的纯净windows系统重装示例
5 0
|
1月前
|
Windows
超详细的纯净windows系统重装示例
本文介绍了如何使用大白菜制作启动盘来安装纯净版Windows系统,以避免安装Ghost版本时附带的不必要的软件。
36 1
|
1月前
|
开发框架 JavaScript 安全
WIndows Server 2016 部署 Web服务(简单篇)
WIndows Server 2016 部署 Web服务(简单篇)
|
1月前
|
安全 关系型数据库 虚拟化
WIndows Server 远程桌面服务—RDS
WIndows Server 远程桌面服务—RDS
|
1月前
|
Windows
Windows2016 搭建 DHCP服务
Windows2016 搭建 DHCP服务
|
1月前
|
监控 安全 虚拟化
深入浅出:NSSM封装Windows服务工具的使用与介绍
深入浅出:NSSM封装Windows服务工具的使用与介绍
77 3
|
1月前
|
监控 安全 C#
开发公司电脑监控软件的报警系统:一个C#示例
在当今数字化时代,企业对其计算机网络和系统的安全性和稳定性越来越重视。为了确保员工遵守公司政策、保护机密信息以及监控系统的正常运行,开发一种可靠的公司电脑监控软件变得至关重要。本文将介绍如何使用C#编写一个简单而有效的报警系统,以便监控关键数据并在必要时发出警报。
100 0

热门文章

最新文章