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,如需转载请自行联系原作者
目录
相关文章
|
3月前
|
开发框架 监控 安全
Windows Defender 导致 Web IIS 服务异常停止排查
某日凌晨IIS服务异常停止,经查为Windows Defender安全补丁KB2267602触发引擎更新,导致系统资源波动,进而引发应用池回收。确认非人为操作,系统无重启。通过分析日志与监控,定位原因为Defender更新后扫描加重负载。解决方案:将IIS及.NET相关路径添加至Defender排除列表,避免业务影响。
528 116
|
11月前
|
开发框架 安全 .NET
掌握 LINQ:通过示例解释 C# 中强大的 LINQ的集运算
通过本文的示例,我们详细介绍了C#中LINQ的强大集合运算功能。LINQ提供了一种简洁、灵活和类型安全的方式来查询和操作数据集合,从而大大提高了代码的可读性和可维护性。希望本文能帮助读者更好地掌握和应用LINQ,提高开发效率。
317 13
|
10月前
|
Linux iOS开发 MacOS
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Git 服务
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Git 服务
317 0
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Git 服务
|
监控 搜索推荐 开发工具
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
1866 2
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
|
人工智能 数据处理 C#
AI Dev Gallery:微软开源 Windows AI 模型本地运行工具包和示例库,助理开发者快速集成 AI 功能
微软推出的AI Dev Gallery,为Windows开发者提供开源AI工具包和示例库,支持本地运行AI模型,提升开发效率。
822 13
|
边缘计算 安全 网络安全
|
网络安全 Windows
Windows server 2012R2系统安装远程桌面服务后无法多用户同时登录是什么原因?
【11月更文挑战第15天】本文介绍了在Windows Server 2012 R2中遇到的多用户无法同时登录远程桌面的问题及其解决方法,包括许可模式限制、组策略配置问题、远程桌面服务配置错误以及网络和防火墙问题四个方面的原因分析及对应的解决方案。
1309 4
|
开发框架 .NET API
Windows Forms应用程序中集成一个ASP.NET API服务
Windows Forms应用程序中集成一个ASP.NET API服务
318 9
|
应用服务中间件 Apache Windows
免安装版的Tomcat注册为windows服务
免安装版的Tomcat注册为windows服务
441 3
|
消息中间件 C# 数据安全/隐私保护
手写MSMQ微软消息队列收发工具类
【10月更文挑战第16天】本示例展示了如何使用C#语言编写MSMQ(微软消息队列)的收发工具类,包括发送和接收消息的方法。通过检查队列是否存在并创建、使用`MessageQueue`类发送和接收消息。示例还提供了简单的调用方式,并提醒用户注意权限管理和异常处理。
174 0