简体   繁体   English

C#多线程委托和事件

[英]C# multi-threaded delegate and events

I'm having some trouble understanding where to go from here - I have developed a form that has access to a SQL Express database and manipulates it via a DataSet. 我在理解从这里去哪里时遇到了一些麻烦-我开发了一种可以访问SQL Express数据库并通过DataSet操作它的表单。 I have also developed a class that listens on a multi-threaded TCP server for updates from other clients via a proprietary protocol. 我还开发了一个类,该类在多线程TCP服务器上侦听通过专有协议从其他客户端进行的更新。

What I need to do is get these updates to the form, which has the instance of the dataset to be updated. 我需要做的是对表单进行这些更新,该表单具有要更新的数据集的实例。

I have checked out some of the event and delegate help on here and this guide that seemed handy at first, but suffers from the same problem that a lot of these examples have - they are not very good at articulating exactly what is happening, and end up using similar variables in both classes. 我已经检查了一些事件,并在此处委托了帮助,而本指南起初似乎很方便,但是却遇到了许多这些示例所遇到的相同问题-它们不太善于准确说明正在发生的事情,然后结束在两个类中使用相似的变量

Which class is the "subscriber", and which is the "Publisher"? 哪个类是“订阅者”,哪个是“发布者”? I really detest forms programming and would do this entirely command-line based, but it is for a group of people who are so computer illiterate that they might experience trauma from having to use a prompt. 我真的很讨厌表单编程,并且会完全基于命令行来执行此操作,但这是针对一群计算机知识不足的人,他们可能因使用提示而遭受创伤。

The publisher is the object whose class declares the event. 发布者是其类声明事件的对象。 For example, a DataTable publishes a RowChanged event. 例如,一个DataTable发布一个RowChanged事件。

The object that handles an event is the subscriber. 处理事件的对象是订户。 For example, a class with a _table field could subscribe to the RowChanged event thus (assuming the handling method exists, of course): 例如,具有_table字段的类可以预订RowChanged事件,从而(当然,假设存在处理方法):

this._table.RowChanged += this.HandleRowChanged;

Sometimes, a class may subscribe to its own event. 有时,一个类可能会订阅自己的事件。 An example would be the Load event of a Windows form. 一个示例是Windows窗体的Load事件。 This uses more overhead than necessary, and some frameworks provide virtual methods to allow subclasses to extend the base class's functionality without creating an event delegate. 这使用了不必要的开销,并且某些框架提供了虚拟方法,以允许子类扩展基类的功能而无需创建事件委托。 That's why the Form's OnLoad method is virtual. 这就是Form的OnLoad方法是虚拟的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM