当前位置: 首页 > news >正文

C# 按照主题的订阅 按照类型的订阅

安装TinyPubSub库,按照 主题发布订阅
在这里插入图片描述

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 static TinyPubSubLib.TinyPubSub;namespace Net9Test
{public partial class Form4 : Form{private readonly string _channelName = "MyChannel";public Form4(){InitializeComponent();RefresUI();RefresUI2();}private void button1_Click(object sender, EventArgs e){Publish(_channelName, Guid.NewGuid().ToString());}public void RefresUI(){Subscribe<string>("MyChannel", data =>{richTextBox1.Invoke(new Action(() =>{richTextBox1.AppendText($"[{DateTime.Now}] 收到消息:{data}\r\n");}));});}public void RefresUI2(){Subscribe<string>(_channelName, data =>{richTextBox2.Invoke(new Action(() =>{richTextBox2.AppendText($"[{DateTime.Now}] 收到消息:{data}\r\n");}));});}private void Form4_FormClosing(object sender, FormClosingEventArgs e){Unsubscribe(_channelName);}}
}

安装PubSub库,按照类型发布 订阅,相当于轻量的MediatR,中介者模式
在这里插入图片描述

using Net9Test.Services;
using PubSub;
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;namespace Net9Test
{public partial class Form3 : Form{Hub hub = Hub.Default;public Form3(){InitializeComponent();RefreshUI();RefreshUI2();}Random random = new Random(DateTime.Now.Microsecond);private void button1_Click_1(object sender, EventArgs e){hub.Publish(random.Next(100, 200));}public void RefreshUI(){hub.Subscribe<int>(this, data =>{richTextBox1.Invoke(new Action(() =>{richTextBox1.AppendText($"[{DateTime.Now}] 收到消息:{data}\r\n");}));});}public void RefreshUI2(){hub.Subscribe<int>(this, data =>{richTextBox2.Invoke(new Action(() =>{richTextBox2.AppendText($"[{DateTime.Now}] 收到消息:{data}\r\n");}));});}private void Form3_FormClosing(object sender, FormClosingEventArgs e){hub.Unsubscribe<int>();}}
}
http://www.dtcms.com/a/283036.html

相关文章:

  • 人工智能与机器人研究|深孔内表面缺陷特征内窥测量方法研究
  • 智能呼叫中心系统:重构客户服务的核心引擎
  • 浅谈车载电控和机器人一体化关节电控区别和联系
  • 【Arm】Fusa Compiler 6.16LTS的安全认证报告获取
  • Openharmony XTS编译运行
  • 2025年Java最新社招面试八股文+技术场景题(金九银十)
  • Linux内核设计与实现 - 第2章 内核开发的准备
  • git的diff命令、Config和.gitignore文件
  • 《机器学习数学基础》补充资料:标准差与标准化
  • Ceph集群OSD崩溃恢复
  • 量子比特耦合与系统集成:量子计算硬件的核心突破
  • 离线部署三节点 Ceph 分布式存储
  • 访问者设计模式
  • Elasticsearch+Logstash+Kibana部署
  • java解析word文档
  • windows内核研究(进程与线程-等待链表和调度链表和线程切换)
  • Excel 导入导出工具类文档
  • ubuntu中拷贝docker容器中的文件到宿主机
  • 万字长文解析 OneCode3.0 AI创新设计
  • 微服务的编程测评系统2
  • 2025年量化交易平台对比:付费与开源方案深度分析
  • nerdctl - 兼容 Docker 语法 的 containerd 命令行界面
  • 17.图像金字塔采样(放大,缩小处理)拉普拉斯金字塔
  • 闰年的历史由来与C语言实现详解
  • 7.16 拓扑排序 | 欧拉回路 |链表排序 前缀和
  • Vue在线预览Excel和Docx格式文件
  • Redis学习其一
  • Python学习之路(十三)-常用函数的使用,及优化
  • Redis读写策略深度解析:高并发场景下的缓存兵法
  • python基础语法9,用os库实现系统操作并用sys库实现文件操作(简单易上手的python语法教学)