在 vb.net 中处理来自多个源的事件和通知到主窗体的最佳方法
我有一个 vb.net 应用程序,其中有一个主窗体。大约有 10 个类,每个类都有自己的功能,如 tcpactions、fileactions、serialport 操作等。用户与主窗体交互并执行某些操作,这些操作将调用这些类中的方法。现在我在主窗体中有一个通知文本区域,我想在通知区域中显示这些类中正在执行的当前操作及其结果。
例如,当用户单击表单中的“启动进程”按钮时,我会调用“ProcessActions”类中的“launchprocess”方法。现在,此方法尝试启动大约 7 个不同的进程,启动后它会发送诸如“进程 1 启动”之类的通知,或者如果失败,它会发送诸如“进程 1 启动失败”之类的通知。
我目前使用事件处理程序并使用它们来显示通知,但是随着我必须处理的事件数量的增加,它变得越来越麻烦,我将来可能需要添加更多的类。那么有没有更好的方法来处理来自其他类的通知。
I have a vb.net application in which there is a main form. There are around 10 classes each having their own functionalities like tcpactions, fileactions, serialport actions etc. The user interacts with the main form and does certain actions which will invoke the methods in these classes. Now I have a notifications textarea in the mainform and i want to show the current action being performed in those classes and their results in the notifications area.
for example when a user clicks a "Start Process" Button in the form i invoke the method "launchprocess" in a class "ProcessActions". Now this method tries to launch about 7 different process and after launching it sends notification such as "process 1 launched" or if it fails it sends notifications such as "process 1 launch failed".
I currently use event handlers and use them to show notifications but with the amount of events i have to handle it is getting cumbersome and i might have to add even more classes in the future. So is there a better way of handling notifications from other classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议在主窗体可以响应的所有类中使用通用的自定义事件处理程序。首先定义一个新的 EventArgs 类来处理您发送回的通知:
接下来定义一个基类来引发通知:
让其他每个类都从该基类继承。然后发送通知就变得非常容易:
最后在主窗体中,监听子类引发的任何事件
I would suggest using a common custom event handler in all of the classes that the main form can respond to. First define a new EventArgs class to handle the notifications you're sending back:
Next define a base class to raise the notifications:
Have each of your other classes inherit from the base class. Then it becomes quite easy to send a notification:
Finally in your main form, listen for any events raised by the child classes