C#之线程ParameterizedThreadStart
今天用到了ParameterizedThreadStart 的实例化对象,但是总提示没有与委托匹配的重载,网上搜索了很多,终于明白什么原因了,在此记录下方便以后查阅。
class Program
{
static void Main(string[] args)
{
Work work = new Work();
//两种实例化委托的方法;
//ParameterizedThreadStart ParameterizedThreadStartDelegate = new ParameterizedThreadStart(work.DoWork);
ParameterizedThreadStart ParameterizedThreadStartDelegate = work.DoWork;
Thread thread = new Thread(ParameterizedThreadStartDelegate);
thread.Start(5); //参数
Console.ReadKey();
}
}
public void DoWork(Object t)
{
for (int i = 0; i <= (int)t; i++)
{
Console.WriteLine("请打印{0}\n", i);
}
}
以上代码是正确的写法,关键就在下图标红处