Windows 事件与跨线程调用

需求

当我们新建一个类,通常会遇到当类的一个属性变化时,如何通知用户?比如串口收到数据,tcp 或UDP 收到网络数据时,如何及时通知用户?

1、查询是否收到,若收到,在文本框显示。

2、专门新增加一个线程,专门负责接收数据。当有数据收到时,通过"数据已收到"事件,用事件处理程序,来更新文本框。

第一种方法简单,但十分占用资源。第二种方法,涉及事件及线程安全问题。

现以第二种方法为例,进行说明。

(1) 在类中声明事件处理程序

java 复制代码
public   delegate void ReiceivedDataChgEventHandler(object sender, ReceivedDataChgEventArgs args); 
public    ReiceivedDataChgEventHandler?  ReceivedDataChg;

(2)在数据的set 方法中,加入事件处理

java 复制代码
public string ReceivedData{
  set
 	{
     string oldValue = receivedData??"";
     string newValue = value??"";
     receivedData = value;
     NewData = true;
     OnReceivedDataChg("receivedData", oldValue, newValue);
 		}
 }
 private string? receivedData;
 public virtual void OnReceivedDataChg(string name, object oldValue, object newValue)
 {
     if (ReceivedDataChg != null)
     {
         ReceivedDataChg.Invoke(this, new ReceivedDataChgEventArgs(name, oldValue, newValue));
     }
 }

3、在form中,新建类,并为ReceivedDataChg增加处理程序。特别要说明的时,由于类form并不时由serialPort类创建的,由类的实例调用form中的控件时,windows 认为类越权了。所以需要由textBox2反过来调用类的事件处理程序 "setText"。

是不是有点绕。

java 复制代码
private void button1_Click(object sender, EventArgs e)
{
    string portName = comboBox1.SelectedItem as string ?? "com1";
    sp = new(portName, 115200, 8, "1", SerialPortC.ParityBits.None);
    sp.ReceivedDataChg += SetText;
    if (sp._serialPort.IsOpen)
    {
        this.textBox1.Text = "AT";
        sp.Writeline("AT");
    }
}
public void SetText(object sender, ReceivedDataChgEventArgs args)
{
    if (this.textBox2.InvokeRequired)
    {
        this.textBox2.Invoke(SetText, this.textBox2, args);
    }
    else
    {
        this.textBox2.AppendText((string)args.NewValue);
    }
}
相关推荐
江湖人称菠萝包3 小时前
【Windows】《深入浅出Windows API程序设计:核心编程篇》笔记-Chapter4-进程
windows·笔记
www.025 小时前
Codex额度用完怎么办?Windows定时自动继续VS Code Codex任务实战
人工智能·windows·vscode·自动化·openai·codex·autohotkey
StarDream-Online8 小时前
Windows 下 uv 安装与配置完全指南(含环境变量设置)
windows·uv
空 白II10 小时前
cuda toolkit 10.0 windows 安装实录
windows
名字还没想好☜10 小时前
Java Collectors.groupingBy 进阶:多级分组、下游收集器与统计聚合一次搞定
java·windows·后端·python·spring
漂着的圆木11 小时前
模型应用Gemini登陆Windows:Alt+Space唤起
windows
csdn_aspnet11 小时前
如何从电脑主机拷东西到VWmare虚拟机
linux·运维·服务器·windows·vmware·虚拟机
Li Ming&1 天前
Windows Server 2019 操作系统《保姆级安装教程》
windows·信息与通信
陈天伟教授1 天前
Windows操作技巧 -03
windows
TechExplorer3651 天前
Windows 隐藏导航窗格【主文件夹】、【图库】、【OneDrive】
windows·onedrive