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#.
A thread can execute a static method without creating an object of the class. The static method is passed directly to the Thread constructor.
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:
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.
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:
The following examples demonstrate different ways to execute methods by using 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.
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.
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.
We request you to subscribe our newsletter for upcoming updates.