C# Microsoft消息队列服务器的使用 MSMQ

先安装消息队列服务器
C# Microsoft消息队列服务器的使用 MSMQ_第1张图片

 private static readonly string path = ".\\Private$\\myQueue";
        private void Create()
        {
            if (!MessageQueue.Exists(path))
            {
                MessageQueue.Create(path);
            }
        }
        private void Send()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            MessageQueue myQueue = new MessageQueue(path);
            myQueue.Send(textBox1.Text);
            stopwatch.Stop();
        }
        private void Display()
        {
            Task.Run(() =>
            {
                MessageQueue rcvMyQueue=new MessageQueue(path);
                rcvMyQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                while (true)
                {
                    var message = rcvMyQueue.Receive();
                    listBox1.BeginInvoke(new Action(() =>
                    {
                        listBox1.Items.Add(DateTime.Now.ToString("HH:mm:ss.fff")+":"+message.Body.ToString());
                    }));
                }
            });
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Create();
            Display();
        }

你可能感兴趣的:(c#,microsoft,服务器)