Return a value from an Event -- is there a Good Practice for this?(从事件中返回一个值——这有什么好的做法吗?)
问题描述
我正在做一个使用异步 TCP 套接字的小型多线程应用程序,但我要直截了当:我正在使用自定义事件从表单中读取值,并且事件使用的委托返回一个完成后的字符串.
I'm doing a small multi-threaded app that uses asynchronous TCP sockets, but I will get to the point: I'm using a custom event to read a value from a form and the delegate used by the event returns a string when finished.
我的问题是:这是正确的吗?从事件中返回值可以吗?还是有更好的方法来做到这一点?(比如使用简单的表单委托来读取值)
My question here is: is that correct? is it OK to return values from the events? or is there a better way to do this? (like using a simple delegate to the form to read the values)
推荐答案
从事件返回值通常很尴尬.在实践中,我发现在传递给事件的一组自定义 EventArgs 中包含可写属性要容易得多,然后在事件触发后进行检查——类似于 WinForms FormClosing 事件的 Cancel 属性.
It's often awkward to return values from events. In practice, I've found it much easier to include a writable property on a set of custom EventArgs that is passed to the event, and then checked after the event fires -- similar to Cancel property of the WinForms FormClosing event.
这篇关于从事件中返回一个值——这有什么好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从事件中返回一个值——这有什么好的做法吗?
基础教程推荐
- JSON.NET 中基于属性的类型解析 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
