C# 多线程传参

时间:2022-01-05 08:29:39
 using System;
using System.Threading; //多线程调试: 2013.10.08
namespace ThreadExample
{
class App
{
public struct Data
{
public string Message;
} //将消息写入控制台
static void ThreadMainWithParameter(object o)
{
Data d = (Data)o;
Console.WriteLine("Running in a thread , receive {0}.",
d.Message);
} public static void Main()
{
var t1 = new Thread(() => Console.WriteLine("Running in a thread, id:{0}",
Thread.CurrentThread.ManagedThreadId));
t1.Start();
Console.WriteLine("This is the main thread, id:{0}",
Thread.CurrentThread.ManagedThreadId); var d = new Data { Message = "Info"};
var t2 = new Thread(ThreadMainWithParameter); //传递变量d
t2.Start(d);
} static void ThreadMain()
{
for (int i = ; i < ;i++ )
{
Console.WriteLine("Running in a thread {0}.",i);
}
}
}
}

输出结果:

C# 多线程传参