当前位置: 首页 > 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.lryc.cn/news/590524.html

相关文章:

  • OCR 与 AI 图像识别:协同共生的智能双引擎
  • Spring MVC中@PathVariable的用法详解
  • Vue 3 中调用子组件方法
  • LLM大语言模型不适合统计算数,可以让大模型根据数据自己建表、插入数据、编写查询sql统计
  • 从洞察到行动:大数据+AI赋能消费者洞察
  • 【前端】HTML语义标签的作用与实践
  • Ubuntu GRUB菜单密码重置教程
  • 重学SpringMVC一SpringMVC概述、快速开发程序、请求与响应、Restful请求风格介绍
  • 【一文解决】块级元素,行内元素,行内块元素
  • 第五章 OB 分布式事务高级技术
  • exports使用 package.json字段控制如何访问你的 npm 包
  • 多人协作游戏中,团队共同获取的装备如何确定按份共有或共同共有
  • 软路由 + 代理 IP 实现多手机不同公网 IP 分配教程
  • 从0开始学习R语言--Day48--Calibration Curves 评估模型
  • JobSet:Kubernetes 分布式任务编排的统一解决方案
  • 【add vs commit】Git 中的 add 和 commit 之间的区别
  • PLUS模型+生态系统服务多情景模拟预测实践技术
  • 大语言模型幻觉检测:语义熵揭秘
  • Reddit Karma是什么?Post Karma和Comment Karma的提升指南
  • 精彩代码分析-1
  • 光伏项目快速获取地址,三种地图赋能设计
  • 倪海厦全套下载,八纲辨证,人纪,天纪,针灸,电子版
  • vue3中高阶使用与性能优化
  • Day04_C语言网络编程20250716
  • Nginx,MD5和Knife4j
  • PHP面向对象编程:类与对象的基础概念与实践
  • Uniapp中双弹窗为什么无法显示?
  • Coze工作流无法更新问题处理
  • React+Next.js+Tailwind CSS 电商 SEO 优化
  • 2_概要设计编写提示词_AI编程专用简化版