结束 BeginInvoke 的正确方法?
我最近阅读了此帖子在 MSDN 上。 因此,我正在考虑使用 lambda 表达式作为调用 EndInvoke 的一种方式,以确保一切都良好且整洁。
哪个更正确?
示例 1:
Action<int> method = DoSomething;
method.BeginInvoke(5, (a)=>{method.EndInvoke(a);}, null);
示例 2:
Action<int> method = DoSomething;
method.BeginInvoke(5, (a)=>
{
Action<int> m = a.AsyncState as Action<int>;
m.EndInvoke(a);
}, method);
I recently read this thread on MSDN. So I was thinking of using a lambda expression as a way of calling EndInvoke just as a way to make sure everything is nice and tidy.
Which would be more correct?
Example 1:
Action<int> method = DoSomething;
method.BeginInvoke(5, (a)=>{method.EndInvoke(a);}, null);
Example 2:
Action<int> method = DoSomething;
method.BeginInvoke(5, (a)=>
{
Action<int> m = a.AsyncState as Action<int>;
m.EndInvoke(a);
}, method);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的第二个示例稍微高效一些,因为不必在闭包中捕获“方法”委托实例。 我怀疑你不会注意到。
Your 2nd example is slightly more efficient because the "method" delegate instance doesn't have to be captured in the closure. I doubt you'd ever notice.
我不知道这在 09 年 1 月是否可行,但现在你可以这样写:
I don't know if this was possible way back in Jan '09, but certainly now you can just write this:
您可能想阅读此内容 Haacked 博客上的帖子。
还没有机会测试它,但要点在最后一行之一:
You might want to read this thread on Haacked's blog.
Haven't had a chance to test it, but the gist is in one of the last lines: