![](/img/trans.png)
[英]ThreadPool.QueueUserWorkItem with a lambda expression and anonymous method
[英]Parameter to QueueUserWorkItem with a lambda expression?
在下面的代码中,参数's'代表什么? 我们不能只省略's',因为它没有在方法中使用,所以我们有一个没有参数的匿名方法,如()=> ...?
ThreadPool.QueueUserWorkItem((s)=>
{
Console.WriteLine("Working on a thread from threadpool");
});
更新1:
根据接受的答案,匿名方法只是正常WaitCallback委托方法的替代,如下面的ocd中那样,QueueUserWorkItem需要它作为参数。 因此,'s'应该是对象类型,因为它是ThreadProc方法的参数。
void ThreadProc(Object stateInfo) {
// No state object was passed to QueueUserWorkItem, so
// stateInfo is null.
Console.WriteLine("Working on a thread from threadpool");
}
匿名委托的C#2.0语法允许省略参数列表,在这种情况下,它将匹配任何一组(非ref
非out
)参数并忽略它们。
ThreadPool.QueueUserWorkItem(delegate {
Console.WriteLine("Working on a thread from threadpool");
});
请注意, delegate {}
与delegate () {}
另一方面,lambda语法在没有提供参数列表的情况下不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.