C# 使用 Windows 消息队列机制
保存消息队列数据的本地磁盘地址:C:\WINDOWS\system32\msmq\storage
注意:
1,要使用 windows 消息队列机制,必须在该 windows 操作系统中先安装“ windows 消息队列” 组件(从操作系统光盘中) ;
2,要使用启用消息队列,必须先启动“Messenger”服务(传输客户端和服务器之间的 NET SEND 和 Alerter 服务消息。此服务与 Windows Messenger 无关。如果服务停止,Alerter 消息不会被传输。如果服务被禁用,任何直接依赖于此服务的服务将无法启动。);
控件:
Form1.Designer.cs
private System.Windows.Forms.Button btnEnumerateMessages;
private System.Windows.Forms.TextBox txtMessages;
private System.Windows.Forms.Button btnRemoveMessages;
private System.Windows.Forms.Button btnSendHighestPriorityMessage;
代码:
代码
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Messaging;
using System.Xml.Serialization;
namespace MessageQueuingExample
{
public partial class Form1 : Form
{
//The . in the queueName represents localhost
private const string queueName = ".\\Private$\\Liuq";
//private const string queueName = "A405\\Private$\\Liuq";
//We need this class-wide to access it in the methods.
MessageQueue queue = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Create the queue if it doesn't exist.
CreateQueue();
}
private void CreateQueue()
{
//Does the queue already exist??
if (MessageQueue.Exists(queueName))
//Yes, it's already there.
queue = new MessageQueue(queueName);
else
//No, we need to create it.
queue = MessageQueue.Create(queueName, false);
}
private void btnSendMessage_Click(object sender, EventArgs e)
{
//Instantiate our MessageContent object.
MessageContent message = new MessageContent("Hello world!");
//Send it to the queue.
queue.Send(message, "Sample Message");
MessageBox.Show("Message sent.", "MSMQ");
}
private void btnEnumerateMessages_Click(object sender, EventArgs e)
{
//Clear the textbox.
this.txtMessages.Clear();
//Get all messages on the queue.
System.Messaging.Message[] messages = queue.GetAllMessages();
//Loop through the messages.
foreach (System.Messaging.Message message in messages)
{
//Set the formatter for the message.
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[1] { typeof(MessageContent) });
//Get the MessageContent object out of the message.
MessageContent content = (MessageContent)message.Body;
//Update the textbox.
this.txtMessages.Text += content.MessageText + " - " + content.CreationDate.ToString() + "\r\n";
}
}
private void btnRemoveMessages_Click(object sender, EventArgs e)
{
//Purge all messages from the queue.
queue.Purge();
MessageBox.Show("Messages purged", "MSMQ");
}
private void btnSendHighestPriorityMessage_Click(object sender, EventArgs e)
{
//Create a XmlSerializer for the object type we're sending.
XmlSerializer serializer = new XmlSerializer(typeof(MessageContent));
//Instantiate a new message.
System.Messaging.Message queueMessage = new System.Messaging.Message();
//Set the priority to Highest.
queueMessage.Priority = MessagePriority.Highest;
//Create our MessageContent object.
MessageContent messageContent = new MessageContent("Hello world - IMPORTANT!");
//Serialize the MessageContent object into the queueMessage.
serializer.Serialize(queueMessage.BodyStream, messageContent);
//Send the message.
queue.Send(queueMessage, "HIGH PRIORITY");
MessageBox.Show("Important message sent.", "MSMQ");
}
}
}
C# 使用 Windows 消息队列机制相关推荐
- android消息队列模型,Android 消息队列机制
在非UI线程使用Handler进行线程通信时,一般都需要进行3个步骤: 创建Looper Looper.prepar() 创建Handler 启动消息循环Looper.loop() 通过这3步,基本就 ...
- Windows消息响应机制之四:PostQuitMessage和GetMessage函数
Windows是消息驱动的操作系统.在Windows环境下编程必须熟练掌握Windows消息响应机制. 今天在练习Win32编程时碰到一个关于GetMessage函数的问题.这个问题之前一直没有引起 ...
- Windows 消息队列
Windows 消息队列 消息队列的功能 使用消息队列的场合 1. 客户端常常从网络上断开连接 2. 客户端和服务端都在线 消息 1. 消息的类型 2. 消息优先级 3. 消息的传递模式 4. 事务消 ...
- Windows消息队列
Windows消息队列(优先队列) 消息队列是Windows系统的基础.对于每个进程,系统维护一个消息队列.如果在进程中有特定事件发生,如点击鼠标.文字改变等,系统将把这个消息加到队列当中.同时,如果 ...
- 7-26 Windows消息队列
7-26 Windows消息队列(25 分) 消息队列是Windows系统的基础.对于每个进程,系统维护一个消息队列.如果在进程中有特定事件发生,如点击鼠标.文字改变等,系统将把这个消息加到队列当中. ...
- 7-26 Windows消息队列(25 分)
7-26 Windows消息队列(25 分) 消息队列是 Windows 系统的基础.对于每个进程,系统维护一个消息队列.如果在进程中有特定事件发生,如点击鼠标.文字改变等,系统将把这个消息加到队列当 ...
- android的消息队列机制
android下的线程,Looper线程,MessageQueue,Handler,Message等之间的关系,以及Message的send/post及Message dispatch的过程. Loo ...
- 5-2 Windows消息队列 (25分)
5-2 Windows消息队列 (25分) 消息队列是Windows系统的基础.对于每个进程,系统维护一个消息队列.如果在进程中有特定事件发生,如点击鼠标.文字改变等,系统将把这个消息加到队列当中 ...
- ZWave 中的消息队列机制
文章主题 文章主题 在我们的日常编程中,对消息队列的需求非常常见,使用一个简洁.高效的消息队列编程模型,对于代码逻辑的清晰性,对于事件处理的高效率来说,是非常重要的.这篇文章就来看看 ZWave ...
- 详谈Windows消息循环机制
一直对windows消息循环不太清楚,今天做个详细的总结,有说错的地方,请务必指出. 用VS2017新建一个win32 Application的默认代码如下: 这里有几个概念,容易混淆: 1.系统: ...
最新文章
- 论文简述 | CamVox: 一种低成本、高精度的激光雷达辅助视觉SLAM系统
- Spring Boot干货系列:(六)静态资源和拦截器处理 | 掘金技术征文
- 跨浏览器设置标签样式
- linux收发十六进制工具,linux下的十六进制编辑器---wxHexEdit
- 接下来学习计划2020.11.9
- 南京工业大学乐学python答案_铁乐学python_day09_作业
- SQL入门语句之LIKE、GLOB和LIMIT
- 解决codeforces访问慢的问题
- php中html写法,细致说明注解三种PHP嵌套HTML的写法_后端开发
- 视频|光学3D测量技术原理及应用
- c语言实现字符串转16进制,C语言实现字符串中(10进制和16进制)转成十进制数(示例代码)...
- 用原生javascript制作日历
- Jmeter的基础讲解
- ppt中的流程图怎么整体移动_教你两招,把复杂的流程图PPT排版简单化
- CVE-2017-0199——首个Microsoft Office RTF漏洞
- 09.mtk背光流程
- steam平台的Don‘t Starve Together 饥荒联机版管理后台
- 软链接解决存储空间不足
- 回顾 丨破解初创科技企业的融资问题思享会
- win10计算机亮度无法调节,Win10电脑无法调节亮度怎么办 Win10系统不能调节屏幕亮度解决方法...