C#使用多线程(后台线程)

55 阅读1分钟

使用语言:C#

环境:.net core 2.0 (当前使用) (支持 .net 所有环境,我就不多说了)

线程和线程池其实都很简单实现。 
让我们来看看C#的线程如何实现:\

using System;
using System.Threading;
namespace 多线程
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread t1 = new Thread(t1xiancheng);
            Thread t2 = new Thread(t2xiancheng);
            Console.WriteLine("上面声明了两个线程");
            Thread.Sleep(1000);
            Console.WriteLine("线程睡眠了1秒");
            t1.Start();
            t2.Start();
            Console.WriteLine("两线程已经执行完");
            
        }
        public static void t1xiancheng()
        {
            Console.WriteLine("我是1号");
        }
        public static void t2xiancheng()
        {
            Console.WriteLine("我是2号");
        }
        
    }
}

参考上面代码实现线程,后台线程其实就是加一条代码\

t1.IsBackground = true;

关于线程池的使用会在日后更新