博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 消息队列 多线程 委托
阅读量:5905 次
发布时间:2019-06-19

本文共 3020 字,大约阅读时间需要 10 分钟。

发消息

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));            }        }    }}

 

转载于:https://www.cnblogs.com/luludongxu/p/4396311.html

你可能感兴趣的文章
awk命令的几个选项注释
查看>>
Windows更改临时文件夹
查看>>
django base (1)
查看>>
iRedMail调整附件大小 & Postfix的bcc(自动转发/邮件备份/监控/归档) 在同一个服务器是有压力...
查看>>
唯识相链由来
查看>>
linux系统的负载与CPU、内存、硬盘、用户数监控shell脚本
查看>>
Percona Toolkit 安装
查看>>
VOD, TVOD, SVOD FVOD的区别(转)
查看>>
元学习法 - XDITE -Xdite 郑伊廷
查看>>
Firewall之iptables篇
查看>>
sed 语法
查看>>
bash编程之:case语句、read与用户交互
查看>>
RHEL6入门系列之二十二,quota磁盘配额管理
查看>>
费用登记系统(小结)
查看>>
Windows Group Policy Startup script is not executed at startup
查看>>
智能指针
查看>>
AIX修改用户密码登录不成功案例分享
查看>>
【Java例题】7.3 线程题3-素数线程
查看>>
openstack组件使用的默认端口
查看>>
c语言简单版坦克大战(AllenEnemyTrank文件)
查看>>