using System;
using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading;namespace ConPra
{ class Program { //主线程结束之后,所有的子线程都结束 public static void Main(string[] Args) { Thread t1; Thread t2; t1 = new Thread(new ThreadStart(PrintfAllDatasThread1)); t2 = new Thread(new ThreadStart(PrintfAllDatasThread2)); t1.IsBackground = true; t2.IsBackground = true; t1.Start();//与下面的代码一样的功能
//while (t1.ThreadState!=ThreadState.Stopped) //{ // Thread.Sleep(20); //}//while (t1.IsAlive)
//{ // Thread.Sleep(20); //} t1.Join();//表示当t1线程运行结束之后,主线程才能执行 Console.WriteLine(t1.ThreadState.ToString() + " " + t1.IsAlive); t2.Start();while (t2.ThreadState != ThreadState.Stopped)
{}
}public static void PrintfAllDatasThread1() { for (int i = 0; i < 10; i++) {
System.Threading.Thread.Sleep(200);
Console.Write("当前线程ID=" + Thread.CurrentThread.ManagedThreadId + " " + i); }}
public static void PrintfAllDatasThread2()
{ for (int i = 0; i < 100; i++) { System.Threading.Thread.Sleep(200); Console.Write("当前线程ID=" + Thread.CurrentThread.ManagedThreadId + " " + i); } } }}