Thread的两种赋值方式

第一种:ParameterizedThreadStart 

 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);
            }
        }

 

posted @ 2022-01-16 12:54  ProZkb  阅读(142)  评论(0编辑  收藏  举报