使用 Lambda 而非真实方法的异步完成事件?

发布于 2024-10-11 05:54:06 字数 532 浏览 3 评论 0原文

最近做了相当多的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失退 2024-10-18 05:54:06

lambda 的一大优点是可以从周围环境中捕获变量的值:

client.LoadClientsCompleted += (sender, e) => 
{
     // My Code 
     // your code can use clientID here
};
client.LoadClientsAsync(clientID); 

One big pro is that lambdas can capture the value of variables from their surroundings:

client.LoadClientsCompleted += (sender, e) => 
{
     // My Code 
     // your code can use clientID here
};
client.LoadClientsAsync(clientID); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文