This program uses the Thread
class in the System.Threading
namespace to get information about the current thread.
In the Main
method, it calls the CurrentThread
property of the Thread
class to get a Thread
object that represents the current thread. It then sets the Name
property of this object to “primarythread”.
Next, it prints the name and status of the current thread to the console using the Name
and IsAlive
properties of the Thread
object. The Name
property returns the name that was given to the thread, and the IsAlive
property returns a boolean value indicating whether the thread is running or not. In this case, the IsAlive
property will always return true
because the main thread is running until the program ends.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | using System; using System.Threading; namespace threading { class Program { static void Main(string[] args) { Console.WriteLine("Current information"); Thread t = Thread.CurrentThread; t.Name = "primarythread"; Console.WriteLine("Thread Name: {0}", t.Name); Console.WriteLine("Thread Status: {0}", t.IsAlive); Console.ReadKey(); } } } |