Tuesday 21 August 2018

C#: Set a name to thread

Every thread has a property 'Name' associated with it, by setting a value to this property, you can set a name to the thread.

Example
Thread t = new Thread(Print);
t.Name = "Printer Thread";

Program.cs
using System.Threading;
using System;

namespace ThreadingTutorial
{
    class HelloWorld
    {
        static void Main()
        {
            Thread t = new Thread(Print);
            t.Name = "Printer Thread";

            t.Start();
        }

        static void Print()
        {
            Console.WriteLine("{0} Executing the print process", Thread.CurrentThread.Name);
        }
    }
}

Output
Printer Thread Executing the print process



Previous                                                 Next                                                 Home

No comments:

Post a Comment