using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace CreateThreadDemo
{
class Program
{
static void Main(string[] args)
{
//为Main函数的线程命名
Thread.CurrentThread.Name = "主程序线程";
//表示此线程开始执行时要调用的方法
Thread t = new Thread(WriteHi);
t.Name = "自定义线程";
t.Start(true );//在启动线程的方法中加入一个参数,该参数即WriteHi方法使用的参数
WriteHi(false); //已正常方式调用WriteHi方法
Console.ReadLine();
}
static void WriteHi(object isTrue)
{
bool hello = (bool)isTrue;
if (hello)
Console.WriteLine("Hello" + Thread.CurrentThread.Name);
else
Console.WriteLine("Hi" + Thread.CurrentThread.Name);
}
}
}
运行结果:
Hi 主程序线程
Hello自定义线程
但是为什么不是:
Hello自定义线程
Hi 主程序线程