📜  C#线程示例:线程名称

📅  最后修改于: 2020-10-31 14:11:12             🧑  作者: Mango

C#线程示例:命名线程

您可以使用Thread类的Name属性来更改或获取线程的名称。让我们看一个示例,其中设置和获取线程的名称。

using System;
using System.Threading;

public class MyThread
{
    public void Thread1()
    {
        Thread t = Thread.CurrentThread;
        Console.WriteLine(t.Name+" is running");
    }
}
public class ThreadExample
{
    public static void Main()
    {
        MyThread mt = new MyThread();
        Thread t1 = new Thread(new ThreadStart(mt.Thread1));
        Thread t2 = new Thread(new ThreadStart(mt.Thread1));
        Thread t3 = new Thread(new ThreadStart(mt.Thread1));
        t1.Name = "Player1";
        t2.Name = "Player2";
        t3.Name = "Player3";
        t1.Start();
        t2.Start();
        t3.Start();
    }
}

输出:

Player1 is running
Player2 is running
Player3 is running