Advertisement
Methods | Description |
---|---|
void Start() |
This method causes a thread to be scheduled for execution. |
void Abort() |
This method usually terminates the thread by raising a ThreadAbortException in the thread on which it is invoked. |
void Sleep(int milliseconds) |
This method suspends the execution of a thread for the specified amount of milliseconds. |
void Join() |
This method blocks the calling thread until the thread represented by this instance terminates. | .
void Interrupt() |
This method interrupts a thread that is in the WaitSleepJoin thread state. |
Properties | Description |
---|---|
Name |
This property allows us to get/set the name of a thread. |
IsAlive |
This property gets a bool value indicating the execution status of the current thread. |
Priority |
This property gets or sets a value indicating the scheduling priority of a thread. |
CurrentThread |
This property gets the reference to the currently executing thread. |
ThreadState |
This property gets the present state of the current thread. The initial value is Unstarted. |
//C# Accessing the default thread, to get and set its name
using System;
using System.Threading;
class ThreadExm
{
public static void Main(String[] ar)
{
//Getting the reference of the default created Main Thread
//By using the CurrentThread property of Thread class
Thread th = Thread.CurrentThread;
//Getting the name of the default created Main Thread
//By using the Name property of Thread class
Console.WriteLine("Default name of the main thread - "+ th.Name);
//Setting the name of the thread
//By using the Name property of Thread class
th.Name = "Main Thread";
//Getting the just given mame to the default created Main Thread
//By using the Name property of Thread class
Console.WriteLine("New name of main thread - "+ th.Name);
}
}
Default name of the main thread -
New name of main thread - Main Thread
Advertisement
//C# Accessing the default thread, to get and set its name
using System;
using System.Threading;
class ThreadExm
{
public static void Main(String[] ar)
{
//Getting the reference of the default created Main Thread
//By using the CurrentThread property of Thread class
Thread th = Thread.CurrentThread;
//Setting the name of the thread
//By using the Name property of Thread class
th.Name = "Main Thread";
//Getting the just given mame to the default created Main Thread
//By using the Name property of Thread class
Console.WriteLine("Name of the thread - "+ th.Name);
Console.WriteLine("The {0} going to sleep for 5 seconds", th.Name);
//Sleep() method may throw InterruptedException, hence a try-catch block is needed
Thread.Sleep(5000);
Console.WriteLine("The {0} awakes and exits", th.Name);
}
}
Name of the thread - Main Thread
The Main Thread going to sleep for 5 seconds
The Main Thread awakes and exits
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement