zoukankan      html  css  js  c++  java
  • C# 消息队列 多线程 委托

    发消息

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Messaging;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            string QueuePath = ".\private$\test";
            IMessageFormatter formatter = new System.Messaging.BinaryMessageFormatter();
    
            private void button1_Click(object sender, EventArgs e)
            {
                MessageQueue queue;
                if (!MessageQueue.Exists(QueuePath))
                {
                    queue = MessageQueue.Create(QueuePath);
                    queue.SetPermissions("Administrators", MessageQueueAccessRights.FullControl);
                    queue.Label = QueuePath;
                }
    
                System.Messaging.Message message = new System.Messaging.Message();
                message.Body = richTextBox1.Text;
                message.Formatter = formatter;
    
    
                if (!MessageQueue.Exists(QueuePath))
                {
                    return;
                }
    
                queue = new System.Messaging.MessageQueue(QueuePath);
                queue.Send(message);
            }
    
        }
    }

    收消息

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Messaging;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            string QueuePath = ".\private$\test";
            IMessageFormatter formatter = new System.Messaging.BinaryMessageFormatter();
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
            private object obj = new object();
            private void MessageQueueReceive()
            {
                if (!MessageQueue.Exists(QueuePath))
                {
                    return;
                }
                MessageQueue queue = new MessageQueue(QueuePath);
                System.Messaging.Message message = queue.Receive();
                message.Formatter = formatter;
                lock (obj)//加锁
                {
                    WriteLog(message.Body.ToString());//给你的控件赋值
                }
                MessageQueueReceive();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Thread myThread = new Thread(new ThreadStart(MessageQueueReceive));
                myThread.Start();
            }
            //定义一个委托
            private delegate void WriteLogHandle(string format);
            //输出
            private void WriteLog(string msg)
            {
                if (richTextBox1.InvokeRequired)
                {
                    richTextBox1.BeginInvoke(new WriteLogHandle(WriteLog), msg);
                }
                else
                {
                    richTextBox1.AppendText(string.Format(msg));
                }
            }
        }
    }
  • 相关阅读:
    从零入门 Serverless | Knative 带来的极致 Serverless 体验
    SpringCloud 应用在 Kubernetes 上的最佳实践 — 高可用(熔断)
    阿里巴巴成立云原生技术委员会,云原生升级为阿里技术新战略
    解构云原生,从概念到落地:阿里云、声网、微博、好未来、CNCF 的专家们怎么看?
    人工智能训练云燧T10
    云计算应用场景分析
    昇腾全栈解决方案
    自动驾驶解决方案架构
    自动驾驶开发云平台业务分析
    ResNet-50模型图像分类示例
  • 原文地址:https://www.cnblogs.com/luludongxu/p/4396311.html
Copyright © 2011-2022 走看看