保存消息队列数据的本地磁盘地址: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 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;

代码:

代码

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,如需转载请自行联系原作者

C# 使用 Windows 消息队列机制相关推荐

  1. android消息队列模型,Android 消息队列机制

    在非UI线程使用Handler进行线程通信时,一般都需要进行3个步骤: 创建Looper Looper.prepar() 创建Handler 启动消息循环Looper.loop() 通过这3步,基本就 ...

  2. Windows消息响应机制之四:PostQuitMessage和GetMessage函数

    Windows是消息驱动的操作系统.在Windows环境下编程必须熟练掌握Windows消息响应机制.  今天在练习Win32编程时碰到一个关于GetMessage函数的问题.这个问题之前一直没有引起 ...

  3. Windows 消息队列

    Windows 消息队列 消息队列的功能 使用消息队列的场合 1. 客户端常常从网络上断开连接 2. 客户端和服务端都在线 消息 1. 消息的类型 2. 消息优先级 3. 消息的传递模式 4. 事务消 ...

  4. Windows消息队列

    Windows消息队列(优先队列) 消息队列是Windows系统的基础.对于每个进程,系统维护一个消息队列.如果在进程中有特定事件发生,如点击鼠标.文字改变等,系统将把这个消息加到队列当中.同时,如果 ...

  5. 7-26 Windows消息队列

    7-26 Windows消息队列(25 分) 消息队列是Windows系统的基础.对于每个进程,系统维护一个消息队列.如果在进程中有特定事件发生,如点击鼠标.文字改变等,系统将把这个消息加到队列当中. ...

  6. 7-26 Windows消息队列(25 分)

    7-26 Windows消息队列(25 分) 消息队列是 Windows 系统的基础.对于每个进程,系统维护一个消息队列.如果在进程中有特定事件发生,如点击鼠标.文字改变等,系统将把这个消息加到队列当 ...

  7. android的消息队列机制

    android下的线程,Looper线程,MessageQueue,Handler,Message等之间的关系,以及Message的send/post及Message dispatch的过程. Loo ...

  8. 5-2 Windows消息队列 (25分)

    5-2 Windows消息队列   (25分) 消息队列是Windows系统的基础.对于每个进程,系统维护一个消息队列.如果在进程中有特定事件发生,如点击鼠标.文字改变等,系统将把这个消息加到队列当中 ...

  9. ZWave 中的消息队列机制

    文章主题 文章主题   在我们的日常编程中,对消息队列的需求非常常见,使用一个简洁.高效的消息队列编程模型,对于代码逻辑的清晰性,对于事件处理的高效率来说,是非常重要的.这篇文章就来看看 ZWave ...

  10. 详谈Windows消息循环机制

    一直对windows消息循环不太清楚,今天做个详细的总结,有说错的地方,请务必指出. 用VS2017新建一个win32 Application的默认代码如下: 这里有几个概念,容易混淆: 1.系统: ...

最新文章

  1. python 双向循环链表实现_python实现双向循环链表基本结构及其基本方法
  2. groovy 字符串截取最后一个_python数据类型总结——数字和字符串
  3. Python str字符串常用到的函数
  4. 总结:JavaEE完整体系架构
  5. Ubuntu 18.04配置 apache https 访问
  6. Python3.2官方文档翻译--标准库概览(一)
  7. 企业真实面试题总结(二)
  8. cad刷新快捷键_掌握了这些实用的CAD技巧,比别人出图快一小时不止
  9. S3C64xx设备树支持
  10. vs2013编译ffmpeg之三十一 vidstab
  11. C盘清理——借助软件TreeSizeFree【网盘分享】(亲测有效)
  12. 使用 SAP UI5 Smart Chart 控件轻松绘制十数种不同类型的专业图表试读版
  13. 宝塔BT面板无法启动修复方法
  14. Message Bus - 消息总线
  15. uIP各部分协议代码的分析
  16. 关于html5外文翻译三千字,论文外文文献翻译3000字左右.pdf
  17. unity科技风UI界面
  18. linx——curl软件安装
  19. 如何更改iTunes备份路径,c盘伤不起啊 win10 命令创建文件夹关联
  20. 最火的腾云驾雾特效软件ae手机版制作飞天特效的教程

热门文章

  1. NYOJ4——ASCII码排序
  2. javascript里new构造函数返回的值
  3. Web 前端开发精华文章推荐(jQuery、HTML5、CSS3)【系列十四】
  4. (转)Moblin V2活动映像安装详解
  5. Linux虚拟机中安装VMware Tools
  6. MFC中给对话框重绘边框
  7. 理解javascript:void(0);和href=#
  8. 转 - 受益终生的十大经典管理学定律
  9. 单webview上拉刷新下拉加载
  10. 第六节 静态的(static)和单例模式