正确的方式结束一个BeginInvoke的? [英] The proper way to end a BeginInvoke?

查看:81
本文介绍了正确的方式结束一个BeginInvoke的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近阅读MSDN上的此线程。所以我想用LAMDA表达式调用EndInvoke会只是作为一种方式,以确保一切是好的,整洁的一种方式。 ?这将是更正确的。



例子1:

 动作< INT>方法= DoSomething的; 

method.BeginInvoke(5中,(a)=> {method.EndInvoke(一);},NULL);



例2:

 动作< INT>方法= DoSomething的; 

method.BeginInvoke(图5(a)=>
{
动作< INT> M = a.AsyncState作为动作< INT取代;
M。 EndInvoke会(一);
},法);


解决方案

您第二个例子是稍微更有效,因为法委托实例没有在封闭被捕获。我怀疑你有没有注意到。


I recently read this thread on MSDN. So I was thinking of using a lamda 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);

解决方案

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.

这篇关于正确的方式结束一个BeginInvoke的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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