从事件中返回一个值——这有什么好的做法吗? [英] Return a value from an Event -- is there a Good Practice for this?

查看:14
本文介绍了从事件中返回一个值——这有什么好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个使用异步 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.

这篇关于从事件中返回一个值——这有什么好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆