C#线程传参的方法总结
线程传参的几种方式:
方式1 委托
使用线程自带的委托传参,示例如下:
static void Main(string[] args)
{
string str = "你好";
Thread th = new Thread(OnThread);
th.Start(str);
Console.Read();
}
static void OnThread(object o)
{
string str = o as string;
if (!string.IsNullOrEmpty(str))
Console.WriteLine(str);
}
运行结果:
注意:使用这种方法传递的参数必须是object类型的,而且需要进行类型转换。