暫停執行緒:Thread.Sleep(TimeSpan物件)
public static void DoStaticWork()
{
string ThreadName = Thread.CurrentThread.Name;
for (int i=1;i<=30;i++)
{
//暫停執行緒2秒
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine(ThreadName + " DoStaticWork:" + i);
}
}
等待執行緒:Thread物件.Join()
public static void Main()
{
Thread ts1 = new Thread(new ThreadStart(DoStaticWork));
ts1.Name = "ts1";
ts1.Start();
//等待執行緒ts1計算完成
ts1.Join();
Thread ts2 = new Thread(Work.DoStaticWork);
ts2.Name = "ts2";
ts2.Start();
}
沒有留言:
張貼留言