示例
class Program
{
static void Main(string[] args)
{
//创建2个线程对象。我们正在使用委托,因为我们需要通过
//线程的参数。
var thread1 = new Thread(new ThreadStart(() => PerformAction(1)));
var thread2 = new Thread(new ThreadStart(() => PerformAction(2)));
//启动线程运行
thread1.Start();
//注意:只要以上一行从线程开始,下一行就开始;
//即使thread1仍在处理中。
thread2.Start();
//等待thread1完成,然后继续
thread1.Join();
//等待thread2完成,然后继续
thread2.Join();
Console.WriteLine("Done");
Console.ReadKey();
}
//帮助演示并行运行的线程的简单方法。
static void PerformAction(int id)
{
var rnd = new Random(id);
for (int i = 0; i < 100; i++)
{
Console.WriteLine("Thread: {0}: {1}", id, i);
Thread.Sleep(rnd.Next(0, 1000));
}
}
}