current thread in csharp

To get the current thread in C#, you can use the Thread.CurrentThread property from the System.Threading namespace. This property returns the Thread object that represents the currently executing thread.

Here's an example code snippet:

main.cs
using System;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        // get the current thread
        Thread currentThread = Thread.CurrentThread;

        // print out some information about the thread
        Console.WriteLine("Thread ID: {0}", currentThread.ManagedThreadId);
        Console.WriteLine("Thread Name: {0}", currentThread.Name);
        Console.WriteLine("Thread Priority: {0}", currentThread.Priority);
        
        Console.ReadLine();
    }
}
503 chars
19 lines

In this example, we use the Thread.CurrentThread property to get the current thread object, and then print out some information about the thread, such as its ID, name, and priority.

gistlibby LogSnag