using System; using System.Threading; namespace test { public class ThreadWork { public static void DoWork() //创建一个函数类似于DWORD Thread1Proc(LPVOID lpParameter); { for (int i = 0; i < 3; i++) { Console.WriteLine("working thread---"); Thread.Sleep(1000); //线程睡眠100ms } } } class ThreadTest { public static void Main() { Console.WriteLine("Main Start--"); ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork); Thread myThread = new Thread(myThreadDelegate); //相当于HThread hThread=CreateThread(NULL,0,Thread1Proc,NULL,0,NULL); myThread.Start();//启动线程 for (int i = 0; i < 3; i++) { Console.WriteLine("In main"); Thread.Sleep(100); //主线程睡眠100ms } myThread.Join(); //如果线程没有完成则无限阻塞直至完成 Console.WriteLine("Main Join"); } } } //上述代码可以改写成下面的代码 using System; using System.Threading; namespace test { public class ThreadWork { public static void DoWork() //创建一个函数类似于DWORD Thread1Proc(LPVOID lpParameter); { for (int i = 0; i < 3; i++) { Console.WriteLine("working thread---"); Thread.Sleep(1000); //线程睡眠100ms } } } class ThreadTest { public static void Main() { Thread myThread = new Thread(ThreadWork.DoWork); //相当于HThread hThread=CreateThread(NULL,0,Thread1Proc,NULL,0,NULL); myThread.Start();//启动线程 Console.WriteLine("这是第一个线程"); } } }