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));
                }
            }
        }
    }
  • 相关阅读:
    ADO.NET Entity Framework(5)esql (二)。
    google首页动态图片代码
    ms sql 聚合事例
    GridView 一些操作
    《狼与狗的故事》
    Asp.net日期字符串格式化显示方法
    解决网爬工具爬取页面信息出现乱码的问题
    esql的查询结果集 ObjectQuery
    去空空格 null sql
    不安装 oracle的客户,就可以使用pl/sql访问远程oracle 数据库的方法
  • 原文地址:https://www.cnblogs.com/luludongxu/p/4396311.html
Copyright © 2011-2022 走看看