Thread Class in C# - GeeksforGeeks

01 Feb, 2019
In C#, a multi-threading system is built upon the Thread class, which encapsulates the execution of threads. This class contains several methods and properties which helps in managing and creating threads and this class is defined under System.Threading namespace.
Characteristics of Thread class:
Thread class is used to create threads.
With the help of Thread class you can create foreground and background thread.
Thread class allows you to set the priority of a thread.
It also provides you the current state of a thread.
It provides the reference of the current executing thread.
It is a sealed class, so it cannot be inherited.
Example:
// C# program to illustrate// ThreadStart constructorusing System;using System.Threading;  public class MXThread {      // Non-static method    public void mythr()    {        for (int J = 0; J < 2; J++) {              Console.WriteLine("My Thread is"+                         " in progress...!!");        }    }}  // Driver Classpublic class GFG {      // Main Method    public static void Main()    {        // Creating object of ExThread class        MXThread obj = new MXThread();          // Creating and initializing thread        // Using thread class and         // ThreadStart constructor        Thread t = new Thread(new ThreadStart(obj.mythr));        t.Start();    }}
Output:
My Thread is in progress...!!
My Thread is in progress...!!

Example:
// C# program to illustrate how to// set and get the priority of threadsusing System;using System.Threading;  class GFG {      // Main Method    static public void Main()    {          // Creating and initializing threads        Thread THR1 = new Thread(work);        Thread THR2 = new Thread(work);        Thread THR3 = new Thread(work);          // Set the priority of threads        THR2.Priority = ThreadPriority.Lowest;        THR3.Priority = ThreadPriority.AboveNormal;        THR1.Start();        THR2.Start();        THR3.Start();          // Display the priority of threads        Console.WriteLine("The priority of Thread 1 is: {0}",                                              THR1.Priority);          Console.WriteLine("The priority of Thread 2 is: {0}",                                              THR2.Priority);          Console.WriteLine("The priority of Thread 3 is: {0}",                                              THR3.Priority);    }      public static void work()    {          // Sleep for 1 second        Thread.Sleep(1000);    }}
Output:
The priority of Thread 1 is: Normal
The priority of Thread 2 is: Lowest
The priority of Thread 3 is: AboveNormal

Example:
// C# program to illustrate the// concept of Join() methodusing System;using System.Threading;  public class MYThread {      // Non-Static method    public void mythr1()    {        Console.WriteLine("1nd thread is Working..!!");        Thread.Sleep(100);    }      // Non-Static method    public void mythr2()    {        Console.WriteLine("2nd thread is Working..!!");    }}  // Driver Classpublic class ThreadExample {      // Main method    public static void Main()    {        // Creating instance of MYThread class        MYThread obj = new MYThread();          // Creating and initializing threads        Thread T1 = new Thread(new ThreadStart(obj.mythr1));        Thread T2 = new Thread(new ThreadStart(obj.mythr2));        T1.Start();          // Join thread        T1.Join();        T2.Start();    }}
Output:
1nd thread is Working..!!
2nd thread is Working..!!

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread?view=netframework-4.7.2#thread-safety
CSharp Multithreading
CSharp Thread Class
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# | Method Overriding
C# Dictionary with examples
Difference between Ref and Out keywords in C#
C# | Delegates
Top 50 C# Interview Questions & Answers
Introduction to .NET Framework
C# | Constructors
Extension Method in C#
C# | Class and Object
C# | Abstract Classes