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

简介:
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  System.Messaging;

namespace  WindowsApplication1
{
         public  partial  class  Form1 : Form
        {
                 public  Form1()
                {
                        InitializeComponent();
                }

                 string  QueuePath =  ".\\private$\\test" ;
                IMessageFormatter formatter =  new  System.Messaging.BinaryMessageFormatter();

                 private   void  button1_Click( object  sender, EventArgs e)
                {
                        CreateMessageQueue(QueuePath);
                        SendMessage(QueuePath, CreateMessage(richTextBox1.Text, formatter));
                }

                 private   void  button2_Click( object  sender, EventArgs e)
                {
                        System.Messaging.Message msg = ReceiveMessage(QueuePath);
                        msg.Formatter = formatter;
                        richTextBox2.Text = msg.Body.ToString();
                }

                 private  System.Messaging.Message CreateMessage( string  text, IMessageFormatter formatter)
                {
                        System.Messaging.Message message =  new  System.Messaging.Message();
                        message.Body = text;
                        message.Formatter = formatter;
                         return  message;
                }

                 private   void  CreateMessageQueue( string  queuePath)
                {
                         if  (!MessageQueue.Exists(queuePath))
                        {
                                MessageQueue queue = MessageQueue.Create(queuePath);
                                queue.SetPermissions( "Administrators" , MessageQueueAccessRights.FullControl);
                                queue.Label = queuePath;
                        }
                }

                 private   bool  SendMessage( string  queuePath, System.Messaging.Message msg)
                {
                         if  (!MessageQueue.Exists(queuePath))
                        {
                                 return   false ;
                        }

                        MessageQueue queue =  new  System.Messaging.MessageQueue(queuePath);
                        queue.Send(msg);
                         return   true ;
                }

                 private  System.Messaging.Message ReceiveMessage( string  queuePath)
                {
                         if  (!MessageQueue.Exists(queuePath))
                        {
                                 return   null ;
                        }

                        MessageQueue queue =  new  MessageQueue(queuePath);
                        System.Messaging.Message message = queue.Receive();
                         return  message;
                }
        }
}










本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/184323,如需转载请自行联系原作者
目录
相关文章
|
12天前
|
监控 搜索推荐 开发工具
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
|
18天前
|
人工智能 数据处理 C#
AI Dev Gallery:微软开源 Windows AI 模型本地运行工具包和示例库,助理开发者快速集成 AI 功能
微软推出的AI Dev Gallery,为Windows开发者提供开源AI工具包和示例库,支持本地运行AI模型,提升开发效率。
67 13
|
2月前
|
网络安全 Windows
Windows server 2012R2系统安装远程桌面服务后无法多用户同时登录是什么原因?
【11月更文挑战第15天】本文介绍了在Windows Server 2012 R2中遇到的多用户无法同时登录远程桌面的问题及其解决方法,包括许可模式限制、组策略配置问题、远程桌面服务配置错误以及网络和防火墙问题四个方面的原因分析及对应的解决方案。
159 4
|
3月前
|
边缘计算 安全 网络安全
|
3月前
|
开发框架 .NET API
Windows Forms应用程序中集成一个ASP.NET API服务
Windows Forms应用程序中集成一个ASP.NET API服务
117 9
|
3月前
|
应用服务中间件 Apache Windows
免安装版的Tomcat注册为windows服务
免安装版的Tomcat注册为windows服务
152 3
|
3月前
|
Java 关系型数据库 MySQL
java控制Windows进程,服务管理器项目
本文介绍了如何使用Java的`Runtime`和`Process`类来控制Windows进程,包括执行命令、读取进程输出和错误流以及等待进程完成,并提供了一个简单的服务管理器项目示例。
64 1
|
3月前
|
消息中间件 C# 数据安全/隐私保护
手写MSMQ微软消息队列收发工具类
【10月更文挑战第16天】本示例展示了如何使用C#语言编写MSMQ(微软消息队列)的收发工具类,包括发送和接收消息的方法。通过检查队列是否存在并创建、使用`MessageQueue`类发送和接收消息。示例还提供了简单的调用方式,并提醒用户注意权限管理和异常处理。
|
3月前
|
弹性计算 关系型数据库 网络安全
阿里云国际版无法连接和访问Windows服务器中的FTP服务
阿里云国际版无法连接和访问Windows服务器中的FTP服务
|
3月前
|
C# 开发工具 Windows
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
102 0