从事件返回一个值 - 有一个良好的实践呢? [英] Return a value from an Event -- is there a Good Practice for this?

查看:94
本文介绍了从事件返回一个值 - 有一个良好的实践呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了使用异步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事件的属性。

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天全站免登陆