使用 Lambda 而非真实方法的异步完成事件?
最近做了相当多的 silverlight 编程并且喜欢它,但当然 silverlight 是异步的,所以我被迫设置一个事件处理程序以在异步完成时调用。这当然很好用,但我只是得到了很多代码...基本上有 2 个方法用于我需要 wcf 服务的每个调用。我最近发现以下内容
client.LoadClientsCompleted += (sender, e) =>
{
// My Code
};
client.LoadClientsAsync(clientID);
似乎有效,它使用 lambda 而不是物理方法。我知道这不会改变技术的工作原理并且它仍然是异步的。但它似乎整理了我的代码很多。
我很想听听关于我应该使用这个的天气的任何评论,使用这两个有什么优点和缺点吗?
正如我所说,将事件直接指向一个新方法也很好用,但从技术上讲,我每次调用都有 2 个方法...代码正在增长:-)
使用 lambda 方式,我至少将回调事件保留在当前的范围内方法,尽管它仅在完成时触发。这似乎让事情变得更容易,但是这种方法有什么问题吗?
Been doing quite a bit of silverlight programming lately and loving it but of course silverlight is async so i am forced to setup an event handler to call when the async is complete. This works great of course but i am just getting lots of code... basically 2 methods for everycall that i require to a wcf service. I recently discovered the following
client.LoadClientsCompleted += (sender, e) =>
{
// My Code
};
client.LoadClientsAsync(clientID);
It seems to work, its using lambdas rather than a physically method. I understand that this doesn't change the working of the technology and its still async. but it seems to tidy up my code quite a lot.
I would love to hear any comments on weather i should be using this, are there any PROS and CONS using either?
As i say point the event directly at a new method works great as well but technically i have 2 methods for every call i make ... The code is growing :-)
Using the lambda way i at least keep my callback event within my current method although it only fires when complete. It seems to make things easier BUT are there any problems with this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
lambda 的一大优点是可以从周围环境中捕获变量的值:
One big pro is that lambdas can capture the value of variables from their surroundings: