1 带参数的委托方法来传递参数
static void Main(string[] args)
{
var d = new Data { Message = "中国" };
//通过带参数的委托方法来传递参数 ParameterizedThreadStart
var t2 = new Thread(ThreadMainWithParameters);
t2.Start(d);
System.Console.ReadLine();
}
static void ThreadMainWithParameters(object o)
{
Data d = (Data)o;
Console.WriteLine("Running in a thread, received {0}", d.Message);
}
2 通过成员方法和带参数的构造函数传参数
class Program
{
static void Main(string[] args)
{
var obj = new MyThread("广东");
var t3 = new Thread(obj.ThreadMain);
t3.Start();
System.Console.ReadLine();
}
}
public class MyThread
{
private string data;
public MyThread(string data)
{
this.data = data;
}
/// <summary>
/// 线程方法
/// </summary>
public void ThreadMain()
{
Console.WriteLine("Running in a thread, data: {0}", data);
}
}
public struct Data
{
public string Message;
}