Thread.CurrentThread Property in C#

A Thread class is responsible for creating and managing a thread in multi-thread programming. It provides a property known as CurrentThread to check the current running thread. Or in other words, the value of this property indicates the current running thread.

Syntax: public static Thread CurrentThread { get; }

Return Value: This property returns a thread that represent the current running thread.

Below programs illustrate the use of CurrentThread property:
.
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// use of CurrentThread property
using System;
using System.Threading;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
        Thread thr;
  
        // Get the reference of main Thread
        // Using CurrentThread property
        thr = Thread.CurrentThread;
        thr.Name = "Main thread";
        Console.WriteLine("Name of current running "+
                            "thread: {0}", thr.Name);
    }
}

chevron_right


Output:

Name of current running thread: Main thread

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// use of CurrentThread property
using System;
using System.Threading;
  
class GFG {
  
    // Display the id of each thread
    // Using CurrentThread and 
    // ManagedThreadId properties
    public static void Myjob()
    {
        Console.WriteLine("Thread Id: {0}"
           Thread.CurrentThread.ManagedThreadId);
    }
  
    // Main method
    static public void Main()
    {
  
        // Creating multiple threads
        ThreadStart value = new ThreadStart(Myjob);
        for (int q = 1; q <= 7; ++q) 
        {
            Thread mythread = new Thread(value);
            mythread.Start();
        }
    }
}

chevron_right


Output:

Thread Id: 3
Thread Id: 8
Thread Id: 9
Thread Id: 6
Thread Id: 5
Thread Id: 7
Thread Id: 4

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.