• MessageQueue 一 简单的创建和读取


    创建一个队列,并写入数据

    在读取出来

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Messaging;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 队列
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                //发送到队列
                //判断是否存在这个队列(如果存在还创建或者不存在就new 这个队列都是会报错的)
                if (!MessageQueue.Exists(".\Private$\MQDemo"))
                {
              //在指定的位置创建一个队列
                    MessageQueue myNewPrivateQueue =
                    MessageQueue.Create(".\Private$\TestPQueue");
                }
                MessageQueue MQueue = new MessageQueue(".\private$\MQDemo");
                //创建一个消息的实体
                System.Messaging.Message Msg = new System.Messaging.Message();
                //为消息的实体赋值
                Msg.Body = "qqqqqqqqq";
                Msg.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
                //发送到消息队列中
                MQueue.Send(Msg);
    
                //--------------------------------------------------------
    
                //从指定的队列中读取数据
                //链接这个队列
                MessageQueue MQueue22 = new MessageQueue(".\private$\MQDemo");
                //拿到队列中的第一条数据(弹出数据,弹出后该数据会消失)
                System.Messaging.Message Msg22 = MQueue22.Receive();
                //反序列化该消息
                Msg22.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
                Console.WriteLine(Msg22.Body.ToString());
    
                ///MessageQueue队列是可以跨进程的所以,上面的代码可以分别放到两个一般处理程序中
                ///一个进行读
                ///一个取
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    求解:块级元素的宽度自适应问题
    list 小练习
    codevs1017乘积最大
    codevs1048石子归并
    luogu1387 最大正方形
    BZOJ1305: [CQOI2009]dance跳舞
    linux下分卷tar.bz文件的合并并解压缩
    ubuntu命令查补
    认识与学习BASH(中)
    认识与学习BASH
  • 原文地址:https://www.cnblogs.com/ansheng/p/5400440.html
Copyright © 2020-2023  润新知