第一种: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);
            }
        }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2021-08-27
  • 2022-12-23
  • 2021-12-05
  • 2021-08-03
猜你喜欢
  • 2022-12-23
  • 2021-07-03
  • 2022-12-23
  • 2022-12-23
  • 2021-10-17
  • 2022-12-23
相关资源
相似解决方案