Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a thread in C#?
Threads are lightweight processes that represent independent execution paths within a program. In C#, threads are created using the Thread class from the System.Threading namespace. Creating threads enables concurrent programming, allowing multiple operations to run simultaneously and improving application efficiency.
Modern operating systems use threads to implement concurrent programming, which saves CPU cycles and increases application performance by allowing multiple tasks to execute in parallel.
Syntax
Following is the basic syntax for creating a thread in C# −
Thread threadName = new Thread(methodName); threadName.Start();
You can also use a ThreadStart delegate to wrap the method −
ThreadStart threadDelegate = new ThreadStart(methodName); Thread threadName = new Thread(threadDelegate); threadName.Start();
Creating a Basic Thread
The following example demonstrates how to create and start a simple thread −
using System;
using System.Threading;
namespace Demo {
class Program {
public static void ThreadFunc() {
Console.WriteLine("Child thread starts");
Console.WriteLine("Child thread executing...");
Console.WriteLine("Child thread ends");
}
static void Main(string[] args) {
ThreadStart childref = new ThreadStart(ThreadFunc);
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(childref);
childThread.Start();
Console.WriteLine("Main thread continues...");
}
}
}
The output of the above code is −
In Main: Creating the Child thread Main thread continues... Child thread starts Child thread executing... Child thread ends
Creating Thread with Parameters
You can pass parameters to a thread using ParameterizedThreadStart delegate −
using System;
using System.Threading;
namespace Demo {
class Program {
public static void ThreadWithParam(object data) {
string message = (string)data;
Console.WriteLine("Thread received: " + message);
for (int i = 1; i <= 3; i++) {
Console.WriteLine("Thread iteration: " + i);
Thread.Sleep(1000);
}
}
static void Main(string[] args) {
ParameterizedThreadStart threadStart = new ParameterizedThreadStart(ThreadWithParam);
Thread paramThread = new Thread(threadStart);
Console.WriteLine("Starting parameterized thread");
paramThread.Start("Hello from Main!");
Console.WriteLine("Main thread finished");
}
}
}
The output of the above code is −
Starting parameterized thread Main thread finished Thread received: Hello from Main! Thread iteration: 1 Thread iteration: 2 Thread iteration: 3
Using Lambda Expressions
Modern C# allows creating threads using lambda expressions for simpler syntax −
using System;
using System.Threading;
namespace Demo {
class Program {
static void Main(string[] args) {
Console.WriteLine("Main thread starting");
Thread lambdaThread = new Thread(() => {
Console.WriteLine("Lambda thread executing");
for (int i = 1; i <= 3; i++) {
Console.WriteLine("Count: " + i);
Thread.Sleep(500);
}
Console.WriteLine("Lambda thread completed");
});
lambdaThread.Start();
Console.WriteLine("Main thread continues");
lambdaThread.Join(); // Wait for thread to complete
Console.WriteLine("All threads finished");
}
}
}
The output of the above code is −
Main thread starting Main thread continues Lambda thread executing Count: 1 Count: 2 Count: 3 Lambda thread completed All threads finished
Thread Methods
| Method | Description |
|---|---|
Start() |
Starts the thread execution |
Join() |
Waits for the thread to complete before continuing |
Sleep(milliseconds) |
Pauses the current thread for specified time |
Abort() |
Terminates the thread (deprecated in .NET Core) |
Conclusion
Creating threads in C# enables concurrent programming and improves application performance. You can create threads using the Thread class with method delegates, parameters, or lambda expressions. Always consider using Join() to coordinate thread completion when needed.
