1、实现方法:参数必须实现为
object类型参数
。使用ParameterizedThreadStart运行在线程上的参数对象。
现在很少用此方法,并且参数只能是一个参数。
static void zhang(object o)
{
Console.WriteLine(o.ToString());
}
static void Main(string[] args)
{
ParameterizedThreadStart ps = new ParameterizedThreadStart(zhang);
Thread thread = new Thread(ps);
thread.Start((object)"hongying");
//参数传递在这里,强制转换为object类型
Thread thread2 = new Thread(new ParameterizedThreadStart(zhang));
thread2.Start((object)"hongying");
Console.WriteLine("hongying",6);
Console.ReadLine();
}
2、
也可以使用Thread类直接创建一个参数线程对象,方法签名中的参数必须是object类型。
参数必须是object类型,只能有一个参数。
static void yong(
object str)
{
Console.WriteLine("这是我的姓名:{0}",str);
}
static void Main(string[] args)
{
Thread thr = new Thread(yong);
thr.Start((object)"红鹰");
}
3、通过调用无参数的方法,把参数事先放到无参的方法中。也就是通过一个
类把参数、方法进行封装。
这种方法主要用在参数较多的线程,对参数的安全性考虑。
class Program
{
static void Main(string[] args)
{
//实列化进程类对象
zhang z = new zhang("红鹰", 22);
Thread thread = new Thread(new ThreadStart(z.ThreadLianxi));
thread.Start();
Console.ReadLine();
}
}
class zhang
{
//要传递过来的进程参数
public string Name { get; set; }
public int Age { get; set; }
//对参数、类进行初始化
public zhang(string name, int age)
{
Name = name;
Age = age;
}
//进程方法签名
public void ThreadLianxi()
{
Console.WriteLine("我的姓名:{0},我的年龄:{1}",Name,Age);
}
}