Open In App

Thread.CurrentThread Property in C#

Last Updated : 13 May, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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);
    }
}


Output:

Name of current running thread: Main thread

Example 2:




// 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();
        }
    }
}


Output:

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

Reference:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads