Windows 消息队列

来源:互联网 发布:一级建造师做题软件 编辑:程序博客网 时间:2024/05/17 22:17

Windows 消息队列提供对“”消息队列“”服务器上的队列访问。

   1。Window 上面的消息队列创建,一般我们创建一个私有队列

       System.Messaging.MessageQueue mq=System.Messaging.MessageQueue.Create(".\private$\test");

      一般在创建时需要做一个判断,确认该队列是否已经存在了

     System.Messaging.MessageQueue.Exists(".\private$\test");

   2. 发送消息

      System.Messaging.MessageQueue mq = new MessageQueue(“”mqname“”);
      System.Messaging.Message mg = new System.Messaging.Message();
      mg.Label = "消息标签";
      mg.Body = “”消息正文“”;
      mq.Send(mg);

 3.消息读取

     MessageQueue mq = new MessageQueue(“”mqname“”);
                if (mq.GetAllMessages().Length>0)
                {
                    System.Messaging.Message mg = mq.Receive(TimeSpan.FromSeconds(5));//读取队列中第一条消息
                    if (mg!=null)
                    {
                        mg.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });//消息序列化对象
                        MessageBox.Show(string.Format("消息标题:[{0}]--消息正文:[{1}]", mg.Label, mg.Body));
                    }
                }

0 0
原创粉丝点击