Thread的两种赋值方式
static void Main(string[] args) { Console.WriteLine("Ends.................................."); //这个是ParameterizedThreadStart的方式 //第一步:是给Thread赋值方法 Thread newThread = new Thread(DoWork); //第二步:Start里 赋值 newThread.Start(42); Console.ReadKey(); } public static void DoWork(object data) { Console.WriteLine("Static thread procedure. Data='{0}'",data); }
第二种:ThreadStart
static void Main(string[] args) { //第一步:实例化类,然后赋值 ThreadWithState ThreadWithState = new ThreadWithState(); ThreadWithState.Data = 12; //第二步:把方法给到这个是ThreadStart ThreadStart threadDelegate = new ThreadStart(ThreadWithState.DoMoreWork); //第三步:把ThreadStart 复制给Thread线程 Thread newThread2 = new Thread(threadDelegate); newThread2.Start(); Console.ReadKey(); } public class ThreadWithState { public static void DoWork() { Console.WriteLine("Static thread procedure."); } public int Data; public void DoMoreWork() { Console.WriteLine("Instance thread procedure. Data={0}", Data); } }
人各有命,上天注定,有人天生为王,有人落草为寇。脚下的路,如果不是你自己的选择,那么旅程的终点在哪,也没人知道。你会走到哪,会遇到谁,都不一定。