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
C# Program to create a Simple Thread
In C#, a thread represents a separate execution path that can run concurrently with other threads. The System.Threading.Thread class allows you to create and manage threads in your application.
To create a simple thread, you define a method that contains the code you want to execute, then create a Thread object using a ThreadStart delegate that points to your method.
Syntax
Following is the syntax for creating a thread −
Thread threadName = new Thread(new ThreadStart(methodName)); threadName.Start();
You can also use the simplified syntax −
Thread threadName = new Thread(methodName); threadName.Start();
Basic Thread Creation
Here's how to create a simple thread that executes a method multiple times −
using System;
using System.Threading;
class Demo {
public void myThread() {
for (int i = 0; i < 3; i++) {
Console.WriteLine("My Thread");
}
}
}
class NewThread {
public static void Main() {
Demo d = new Demo();
Thread thread = new Thread(new ThreadStart(d.myThread));
thread.Start();
thread.Join(); // Wait for thread to complete
Console.WriteLine("Main thread finished");
}
}
The output of the above code is −
My Thread My Thread My Thread Main thread finished
Using Lambda Expression
You can also create threads using lambda expressions for simpler code −
using System;
using System.Threading;
class Program {
public static void Main() {
Thread thread = new Thread(() => {
for (int i = 1; i <= 5; i++) {
Console.WriteLine($"Thread execution: {i}");
Thread.Sleep(1000); // Pause for 1 second
}
});
thread.Start();
Console.WriteLine("Main thread continues...");
thread.Join();
Console.WriteLine("Thread completed");
}
}
The output of the above code is −
Main thread continues... Thread execution: 1 Thread execution: 2 Thread execution: 3 Thread execution: 4 Thread execution: 5 Thread completed
Multiple Threads Example
Here's an example showing multiple threads running concurrently −
using System;
using System.Threading;
class MultiThreadDemo {
public static void WorkerThread(object threadName) {
for (int i = 1; i <= 3; i++) {
Console.WriteLine($"{threadName}: Task {i}");
Thread.Sleep(500);
}
}
public static void Main() {
Thread thread1 = new Thread(WorkerThread);
Thread thread2 = new Thread(WorkerThread);
thread1.Start("Thread-A");
thread2.Start("Thread-B");
thread1.Join();
thread2.Join();
Console.WriteLine("All threads completed");
}
}
The output of the above code is −
Thread-A: Task 1 Thread-B: Task 1 Thread-A: Task 2 Thread-B: Task 2 Thread-A: Task 3 Thread-B: Task 3 All threads completed
Key Methods
| Method | Description |
|---|---|
Start() |
Begins thread execution |
Join() |
Waits for the thread to complete |
Sleep(milliseconds) |
Pauses the current thread for specified time |
Abort() |
Terminates the thread (deprecated in .NET Core) |
Conclusion
Creating threads in C# is straightforward using the Thread class and ThreadStart delegate. Use Start() to begin execution and Join() to wait for completion. Threads enable concurrent execution, improving application performance for CPU-intensive or I/O operations.
