C# Threading Examples

Last Updated : 2 Jul 2026

Threads allow a program to perform one or more tasks simultaneously. In C#, a thread can execute both static and instance methods that makes it easier to perform background or parallel operations.

Here, you will learn how to execute static methods, instance methods, and different methods by using multiple threads in C#.

Executing Static Methods with Threads

A thread can execute a static method without creating an object of the class. The static method is passed directly to the Thread constructor.

Syntax

The following syntax is used to execute a static method in a new thread:

Thread thread = new Thread(new ThreadStart(ClassName.MethodName));
thread.Start();

In this syntax:

  • Thread: It specifies the class used to create a new thread.
  • thread: It specifies the thread object.
  • ThreadStart: It specifies the delegate that points to the method to execute.
  • ClassName.MethodName: It specifies the static method that runs in the new thread.
  • Start(): It starts the execution of the thread.

Executing Instance Methods with Threads

To execute a non-static (instance) method, you must first create an object of the class and then pass the method to the Thread constructor.

Syntax

The following syntax is used to execute an instance method in a new thread:

ClassName obj = new ClassName();

Thread thread = new Thread(new ThreadStart(obj.MethodName));
thread.Start();

In this syntax:

  • ClassName: It specifies the class that contains the instance method.
  • obj: It specifies the object of the class.
  • MethodName: It specifies the instance method executed by the thread.
  • ThreadStart: It specifies the delegate that points to the method.
  • Start(): It starts the execution of the thread.

Examples of Thread Execution in C#

The following examples demonstrate different ways to execute methods by using threads.

Example 1: Executing a Static Method in Multiple Threads

This example shows how to execute the same static method simultaneously by using two different threads.

using System;
using System.Threading;
public class MyThread
{
    public static void Thread1()
    {
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(i);
        }
    }
}
public class ThreadExample
{
    public static void Main()
    {
        Thread t1 = new Thread(new ThreadStart(MyThread.Thread1));
        Thread t2 = new Thread(new ThreadStart(MyThread.Thread1));
        t1.Start();
        t2.Start();
    }
}

Output:

0
1
2
3
4
5
0
1
2
3
4
5
6
7
8
9
6
7
8
9

Explanation

In this example, we create two thread objects and assign the same static method to both of them. After calling the Start() method, both threads begin executing simultaneously. Since thread scheduling is managed by the operating system, the output order may vary each time the program runs.

Example 2: Executing an Instance Method in Multiple Threads

This example demonstrates how to execute the same instance method by creating an object of the class and assigning it to multiple threads.

using System;
using System.Threading;
public class MyThread
{
    public void Thread1()
    {
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(i);
        }
    }
}
public class ThreadExample
{
    public static void Main()
    {
        MyThread mt = new MyThread();
        Thread t1 = new Thread(new ThreadStart(mt.Thread1));
        Thread t2 = new Thread(new ThreadStart(mt.Thread1));
        t1.Start();
        t2.Start();
    }
}

Output:

0
1
2
3
4
5
0
1
2
3
4
5
6
7
8
9
6
7
8
9

Explanation

In this example, we first create an object of the MyThread class. Next, we create two threads and assign the instance method to both of them. After starting the threads, both execute the same method at the same time. The output order may differ because of thread scheduling.

Example 3: Executing Different Tasks in Different Threads

This example shows how to execute different methods simultaneously by assigning a different task to each thread.

using System;
using System.Threading;

public class MyThread
{
    public static void Thread1()
    {
        Console.WriteLine("task one");
    }
    public static void Thread2()
    {
        Console.WriteLine("task two");
    }
}
public class ThreadExample
{
    public static void Main()
    {
        Thread t1 = new Thread(new ThreadStart(MyThread.Thread1));
        Thread t2 = new Thread(new ThreadStart(MyThread.Thread2));
        t1.Start();
        t2.Start();
    }
}

Output:

task one
task two

Explanation

In this example, we create two threads and assign a different static method to each thread. When both threads are started, they execute their assigned tasks independently. Since each thread performs a different operation, multiple tasks can run simultaneously.


Next TopicC# Thread Sleep