Information about current thread can be obtained using Thread.CurrentThread static property.
Thread.CurrentThread returns the Thread Object related to current thread.
Thread object gives the following information about the thread.
- Name
- ManagedThreadId
- IsBackground
- IsThreadPoolThread
- IsAlive
- Priority
- ThreadState
Sample Code:
using System;
using System.Threading;// Current thread info
class Program {
static void Main(string[] args) {
Console.WriteLine("Main Started");
var th = new Thread(Greet); // Create new thread
th.Start(new object[] { "Bob", "Good Morning" }); // Execute Greet method in the created thread
Console.ReadKey();
}private static void Greet(object info) {
Console.WriteLine("Hi {0}, {1}!", (info as object[])[0], (info as object[])[1]);
Console.WriteLine("\tThread Name: " + Thread.CurrentThread.Name);
Console.WriteLine("\tThread Id: " + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("\tIs Background: " + Thread.CurrentThread.IsBackground);
Console.WriteLine("\tIs ThreadPool Thread: " + Thread.CurrentThread.IsThreadPoolThread);
Console.WriteLine("\tIs Alive: " + Thread.CurrentThread.IsAlive);
Console.WriteLine("\tPriority: " + Thread.CurrentThread.Priority);
Console.WriteLine("\tThread State: " + Thread.CurrentThread.ThreadState);
}
}
Output
Main Started
Hi Bob, Good Morning!
Thread Name:
Thread Id: 11
Is Background: False
Is ThreadPool Thread: False
Is Alive: True
Priority: Normal
Thread State: Running
No comments:
Post a Comment