C# 结束初恋的正确方法是什么?
我最近很紧张。因此,我考虑使用lambda表达式作为调用EndInvoke的一种方式,以确保一切都是好的和整洁的。哪个更正确 例1:C# 结束初恋的正确方法是什么?,c#,lambda,C#,Lambda,我最近很紧张。因此,我考虑使用lambda表达式作为调用EndInvoke的一种方式,以确保一切都是好的和整洁的。哪个更正确 例1: Action<int> method = DoSomething; method.BeginInvoke(5, (a)=>{method.EndInvoke(a);}, null); 作用方法=剂量测定法; method.BeginInvoke(5,(a)=>{method.EndInvoke(a);},null); 例2: Action&
Action<int> method = DoSomething;
method.BeginInvoke(5, (a)=>{method.EndInvoke(a);}, null);
作用方法=剂量测定法;
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);
作用方法=剂量测定法;
方法.开始激活(5,(a)=>
{
动作m=a.AsyncState作为动作;
m、 (一);
}方法);
您可能想阅读Haacked博客上的帖子。
还没有机会测试它,但要点在最后一行中:
ThreadPool.QueueUserWorkItem(callback => im.Send(to, from, subject, body));
您的第二个示例稍微更有效,因为“方法”委托实例不必在闭包中捕获。我怀疑你是否注意到。我不知道这在09年1月是否可行,但现在你肯定可以写下:
method.BeginInvoke(5, method.EndInvoke, null);
实际上我已经读过那篇文章,如果可以在调用BeginInvoke时传入lambda,那么使用QueueUserWorkItem似乎有点多余。QueueUserWorkItem除了调用EndInvoke外,还可以提供其他高级线程池管理功能。最好使用它,这取决于你的用途。在IIS环境中,使用ThreadPool将使用可用于请求处理的相同线程。