dotnet 创建线程,并执行线程
环境.net core 2.1.5
核心代码
Thread thread = new Thread(TConsoleWrite);//记得要在new 创建的时候带上需要执行的方法名
thread.IsBackground = true;//这个是把我们这个线程放到后台线程里面执行
thread.Start();//Start就代表开始执行线程
记得写上等待哟:
不然主线程运行完就会释放掉了
完整代码:
using System;
using System.Threading;
namespace netcore.thread.demo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("开始运行线程 ! ");
//创建线程,命名为thread
Thread thread = new Thread(TConsoleWrite);//记得要在new 创建的时候带上需要执行的方法名
thread.IsBackground = true;//这个是把我们这个线程放到后台线程里面执行
thread.Start();//Start就代表开始执行线程
System.Console.WriteLine("按 ctrl + c 结束运行");
//下面这个循环用于等待线程运行
while (true)
{
}
}
static void TConsoleWrite()
{
//被执行的线程内的方法输出
System.Console.WriteLine("线程一输出");
}
}
}