多线程三种传值方式
记下,以备后查.
static void TestThread4()
{
//1.直接写方法
Thread t1 = new Thread(PrintMessage);
t1.Start("Test Thread1");
//2.使用ParameterizedthreadStart
Thread t2 = new Thread(new ParameterizedThreadStart(PrintMessage));
t2.Start("Test Thread2");
//3. 使用Lambda
Thread t3 = new Thread(() => Print("Test Thread3"));
t3.Start();
//4.使用匿名函数
Thread t4 = new Thread(delegate() { Print("Test thread4"); });
t4.Start();
}
//只能为Object,只能有一个参数
static void PrintMessage(object msg)
{
string sMsg = (string)msg;
Console.WriteLine(sMsg);
Thread.Sleep(500);
}
static void Print(string msg)
{
Console.WriteLine(msg);
Thread.Sleep(500);
}
{
//1.直接写方法
Thread t1 = new Thread(PrintMessage);
t1.Start("Test Thread1");
//2.使用ParameterizedthreadStart
Thread t2 = new Thread(new ParameterizedThreadStart(PrintMessage));
t2.Start("Test Thread2");
//3. 使用Lambda
Thread t3 = new Thread(() => Print("Test Thread3"));
t3.Start();
//4.使用匿名函数
Thread t4 = new Thread(delegate() { Print("Test thread4"); });
t4.Start();
}
//只能为Object,只能有一个参数
static void PrintMessage(object msg)
{
string sMsg = (string)msg;
Console.WriteLine(sMsg);
Thread.Sleep(500);
}
static void Print(string msg)
{
Console.WriteLine(msg);
Thread.Sleep(500);
}
点击下载