How to create Threads in C#

24 Jan, 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. The System.Threading namespace provides classes and interfaces that are used in multi-thread programming.
Some commonly used classes in this namespace are :
Steps to create a thread in a C# Program:
First of all import System.Threading namespace, it plays an important role in creating a thread in your program as you have no need to write the fully qualified name of class everytime.Using System;
Using System.Threading
Now, create and initialize the thread object in your main method.public static void main()
{
    Thread thr = new Thread(job1);
}
OrYou can also use ThreadStart constructor for initializing a new instance.public static void main()
{
     Thread thr = new Thread(new ThreadStart(job1));
}
Now you can call your thread object.public static void main()
{
    Thread thr = new Thread(job1);
    thr.Start();
}

First of all import System.Threading namespace, it plays an important role in creating a thread in your program as you have no need to write the fully qualified name of class everytime.Using System;
Using System.Threading

Using System;
Using System.Threading

Now, create and initialize the thread object in your main method.public static void main()
{
    Thread thr = new Thread(job1);
}
OrYou can also use ThreadStart constructor for initializing a new instance.public static void main()
{
     Thread thr = new Thread(new ThreadStart(job1));
}

public static void main()
{
    Thread thr = new Thread(job1);
}

Or
You can also use ThreadStart constructor for initializing a new instance.
public static void main()
{
     Thread thr = new Thread(new ThreadStart(job1));
}

Now you can call your thread object.public static void main()
{
    Thread thr = new Thread(job1);
    thr.Start();
}

public static void main()
{
    Thread thr = new Thread(job1);
    thr.Start();
}

Below programs illustrate the practical implementations of above steps:
Example 1:
// C# program to illustrate the// creation of thread using// non-static methodusing System;using System.Threading;  public class ExThread {      // Non-static method    public void mythread1()    {        for (int z = 0; z < 3; z++) {            Console.WriteLine("First Thread");        }    }}  // Driver Classpublic class GFG {      // Main Method    public static void Main()    {        // Creating object of ExThread class        ExThread obj = new ExThread();          // Creating thread        // Using thread class        Thread thr = new Thread(new ThreadStart(obj.mythread1));        thr.Start();    }}
Output:
First Thread
First Thread
First Thread

Explanation: In the above example, we have a class named as ExThread that contain a non-static method named as mythread1(). So we create an instance, i.e. obj of ExThread class and refer it in the constructor of ThreadStart class as given in this statement Thread a = new Thread(new ThreadStart(obj.mythread1));. Using Thread a = new Thread(new ThreadStart(obj.mythread1)); statement we will create a thread named as thr and initialize the work of this thread. By using thr.Start(); statement.
Example 2:
// C# program to illustrate the creation// of thread using static methodusing System;using System.Threading;  public class ExThread {      // Static method for thread a    public static void thread1()    {        for (int z = 0; z < 5; z++) {            Console.WriteLine(z);        }    }      // static method for thread b    public static void thread2()    {        for (int z = 0; z < 5; z++) {            Console.WriteLine(z);        }    }}  // Driver Classpublic class GFG {      // Main method    public static void Main()    {        // Creating and initializing threads        Thread a = new Thread(ExThread.thread1);        Thread b = new Thread(ExThread.thread2);        a.Start();        b.Start();    }}
Output :
0
1
2
3
4
0
1
2
3
4

Explanation: In the above example, we have a class named as ExThread and contain two static methods named as thread1() and thread2(). So we do not need to create an instance of ExThread class. Here we call these methods using a class name, like ExThread.thread1, ExThread.thread2. By using Thread a = new Thread(ExThread.thread1); statement we create and initialize the work of thread a, similarly for thread b. By using a.Start(); and b.Start(); statements, a and b threads scheduled for execution.
Note: The output of these programs may vary due to context switching.
CSharp Multithreading
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
Introduction to .NET Framework
C# | Delegates
C# | Multiple inheritance using interfaces
Differences Between .NET Core and .NET Framework
C# | Method Overriding
C# | Data Types
C# | Constructors
C# | String.IndexOf( ) Method | Set - 1
C# | Class and Object