我正在尝试以这种方式使用该方法:
public void Method()
{
ThreadPool.QueueUserWorkItem(() =>
{
while(!paused)
{
ThreadPool.QueueUserWorkItem(() => {...);
}
});
}
}
问题来了,因为它在第一次调用时抛出了一个编译错误。
error CS1593: Delegate
System.Threading.WaitCallback' does not take
0' arguments
知道如何在没有参数的情况下做到这一点吗? , 还有其他选择吗?
最佳答案
您可以只为 lambda 表达式提供一个参数,然后忽略它:
ThreadPool.QueueUserWorkItem(ignored =>
{
while(!paused)
{
ThreadPool.QueueUserWorkItem(alsoIgnored => {...});
}
});
或者改用匿名方法:
ThreadPool.QueueUserWorkItem(delegate
{
while(!paused)
{
ThreadPool.QueueUserWorkItem(delegate {...});
}
});
如果您不关心匿名方法的参数,则不必声明它们。
关于c# - ThreadPool.QueueUserWorkItem 用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9867566/