我想在线程池中运行一个方法。虽然建下面的代码它给出了一个错误在线程池中使用方法
没有重载“方法”匹配委托“System.Threading.WaitCallback”。
我知道哪里有错误发生,但我不知道为什么:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Thread_Pool
{
class Program
{
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(PrintNumbers));
// PrintNumbers();
}
static void PrintNumbers()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
Thread.Sleep(3000);
}
}
}
}
当上面的代码被改写为以下,它工作正常。
static void PrintNumbers(object Stateinfo)
为什么会发生这种情况?可以使用另一种类型(如int
,float
)来代替使用对象?