C# 关于获取线程Id 代码块

C# 关于获取线程Id 代码块

       #region 线程Id获取代码块
        static void Main(string[] args)
        {
            Thread t1 = new Thread(ThreadFun);
            t1.IsBackground = true;//设置为后台线程,避免线程阻塞进程关闭
            t1.Start();

            Console.WriteLine("主线程Id:{0}", Thread.CurrentThread.ManagedThreadId);//获取当前线程Id
            Console.ReadKey();
        }


        public static void ThreadFun()
        {
            long sum = 0;
            for (int i = 0; i < 100000000; i++)
            {
                sum += i;
            }
            Console.WriteLine($"计算结果:{sum}");
            Console.WriteLine("子线程Id:{0}", Thread.CurrentThread.ManagedThreadId);//获取当前线程Id
        }
        #endregion

你可能感兴趣的:(C#,c#,开发语言)