C# 使用 Windows 消息队列机制
保存消息队列数据的本地磁盘地址:C:\WINDOWS\system32\msmq\storage
注意:
1,要使用 windows 消息队列机制,必须在该 windows 操作系统中先安装“ windows 消息队列” 组件(从操作系统光盘中) ;
2,要使用启用消息队列,必须先启动“Messenger”服务(传输客户端和服务器之间的 NET SEND 和 Alerter 服务消息。此服务与 Windows Messenger 无关。如果服务停止,Alerter 消息不会被传输。如果服务被禁用,任何直接依赖于此服务的服务将无法启动。);
控件:

private System.Windows.Forms.Button btnSendMessage;
private System.Windows.Forms.Button btnEnumerateMessages;
private System.Windows.Forms.TextBox txtMessages;
private System.Windows.Forms.Button btnRemoveMessages;
private System.Windows.Forms.Button btnSendHighestPriorityMessage;
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;
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");
}
}
}
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");
}
}
}
作者: XuGang 网名:钢钢 |
出处: http://xugang.cnblogs.com |
声明: 本文版权归作者和博客园共有。转载时必须保留此段声明,且在文章页面明显位置给出原文连接地址! |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架