.NET ez 2011-11-30
如果要判斷Thread執行完畢,才繼續執行程式,此時可以使用Join即可達到等待作用。
using System;
using System.Collections.Generic;
using System.Threading;
namespace Test
{
class Program
{
static void Main(string[] args)
{
List<Thread> List_Thread = new List<Thread>();
for (int i = 0; i < 10; i++)
{
//建立執行緒
List_Thread.Add(new Thread((object x) =>
{
int temp = 3;
while (temp-- > 0)
{
Console.WriteLine("Thread ID :" + x.ToString() + "\t" + temp.ToString());
System.Threading.Thread.Sleep(1000);
}
}));
List_Thread[i].Start(i + 1);
}
//等待所有執行緒完成
foreach (Thread t in List_Thread) t.Join();
Console.WriteLine("End");
}
}
}標籤: .NET
