C# 使用 Windows 消息队列机制

简介:

保存消息队列数据的本地磁盘地址:C:\WINDOWS\system32\msmq\storage


注意:

   1,要使用 windows 消息队列机制,必须在该 windows 操作系统中先安装“ windows 消息队列” 组件系统光盘中) ;

   2,要使用启用消息队列,必须先启动“Messenger服务(传输客户端和服务器之间的 NET SEND 和 Alerter 服务消息。此服务与 Windows Messenger 无关。如果服务停止,Alerter 消息不会被传输。如果服务被禁用,任何直接依赖于此服务的服务将无法启动。);

 

控件:

复制代码
ExpandedBlockStart.gif Form1.Designer.cs
         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;
复制代码


代码:

复制代码
ExpandedBlockStart.gif 代码
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 " );
        }
    }
}



本文转自钢钢博客园博客,原文链接:http://www.cnblogs.com/xugang/archive/2010/04/24/1719299.html,如需转载请自行联系原作者

相关文章
|
30天前
|
Java 数据库 C#
C#winforms实现windows窗体人脸识别
C#winforms实现windows窗体人脸识别
29 0
|
2月前
|
自然语言处理 C# Windows
C#开源免费的Windows右键菜单管理工具
C#开源免费的Windows右键菜单管理工具
|
7月前
|
人工智能 搜索推荐 C#
C#开源且免费的Windows桌面快速预览神器 - QuickLook
C#开源且免费的Windows桌面快速预览神器 - QuickLook
159 0
|
4月前
|
安全 C# Windows
C#开源的一个能利用Windows通知栏背单词的软件 - ToastFish
C#开源的一个能利用Windows通知栏背单词的软件 - ToastFish
|
1月前
|
消息中间件 存储 Cloud Native
【Spring云原生系列】Spring RabbitMQ:异步处理机制的基础--消息队列 原理讲解+使用教程
【Spring云原生系列】Spring RabbitMQ:异步处理机制的基础--消息队列 原理讲解+使用教程
|
3月前
|
API C# C++
微软官方发布的C#开源、免费、实用的Windows工具箱
微软官方发布的C#开源、免费、实用的Windows工具箱
|
4月前
|
JavaScript Linux C#
【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
65 0
|
4月前
|
C# Windows
C#安装“Windows 窗体应用(.NET Framework)”
C#安装“Windows 窗体应用(.NET Framework)”
50 0
|
5月前
|
监控 C# Windows
内网桌面监控软件中的远程控制功能实现(基于C#和Windows Forms)
近年来,随着远程办公的兴起,对内网桌面监控软件的需求逐渐增加。本文将探讨如何通过C#和Windows Forms实现内网桌面监控软件中的远程控制功能,并在结尾部分介绍监控到的数据如何自动提交到网站。
279 0
|
7月前
|
缓存 网络协议 C#
C#开源、功能强大、免费的Windows系统优化工具 - Optimizer
C#开源、功能强大、免费的Windows系统优化工具 - Optimizer