Thread.CurrentThread Property in C#
Last Updated :
13 May, 2019
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:
using System;
using System.Threading;
class GFG {
static public void Main()
{
Thread thr;
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:
using System;
using System.Threading;
class GFG {
public static void Myjob()
{
Console.WriteLine( "Thread Id: {0}" ,
Thread.CurrentThread.ManagedThreadId);
}
static public void Main()
{
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:
Share your thoughts in the comments
Please Login to comment...