MessageQueue c# 例子

            //No, we need to create it.
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;

namespace FormTest
{
    public partial class Form1 : Form
    {
        private List obeservers = new List();

        private const string queueName = ".\\Private$\\Liuq";
        MessageQueue queue = null;

        public Form1()
        {
            InitializeComponent();
            CreateQueue();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageContent mc = new MessageContent();
            mc.Content = "corning";
            queue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MessageContent) });
            queue.Send(mc, "hello");
        }


        private void CreateQueue()
        {
            if (MessageQueue.Exists(queueName))
                queue = new MessageQueue(queueName);
            else
            {
                queue = MessageQueue.Create(queueName, false);
               // queue.SetPermissions("Everyone", MessageQueueAccessRights.FullControl);
                queue.ReceiveCompleted += new ReceiveCompletedEventHandler(messageQueue_ReceiveCompleted);
                queue.BeginReceive();
            }
        }

        public void Add(IAbserver abeserver)
        {
            if (!obeservers.Contains(abeserver))
            {
                obeservers.Add(abeserver);
            }
        }

        public void Delete(IAbserver abeserver)
        {
            if (obeservers.Contains(abeserver))
            {
                obeservers.Remove(abeserver);
            }
        }

      //  MessageQueue messageQueue;

        private void button2_Click(object sender, EventArgs e)
        {
         
        }

        void messageQueue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
        {
            try
            {
                MessageQueue mq = sender as MessageQueue;
                if (null != mq)
                {
                 System.Messaging.Message me = mq.EndReceive(e.AsyncResult);
                 if (null != me.Body)
                 {
                     MessageContent content = me.Body as MessageContent;
                     MessageBox.Show(content.Content);
                 }
                 mq.BeginReceive();
                }
            }
            catch (Exception mess)
            {
                MessageBox.Show(mess.Message);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageQueue.Delete(queueName);
        }
    }

    [Serializable]
    public class MessageContent
    {
        private string _content;

        public String Content
        {
            set { _content = value; }
            get { return this._content; }
        }

        public MessageContent()
        { 
        
        }

        public MessageContent(string content)
        {
            this._content = content;
        }
    }
}


你可能感兴趣的:(MessageQueue c# 例子)