0% found this document useful (0 votes)
1 views14 pages

Multithreading in CSharp With More Code

The document provides an overview of multithreading in C#, explaining concepts such as threads, thread lifecycle, synchronization, and the differences between threads and tasks. It emphasizes best practices like using async/await for non-blocking operations and demonstrates examples of thread management and synchronization techniques. Additionally, it highlights the use of the ThreadPool and the Task class for efficient thread handling and value returns.

Uploaded by

Dharna Ahuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views14 pages

Multithreading in CSharp With More Code

The document provides an overview of multithreading in C#, explaining concepts such as threads, thread lifecycle, synchronization, and the differences between threads and tasks. It emphasizes best practices like using async/await for non-blocking operations and demonstrates examples of thread management and synchronization techniques. Additionally, it highlights the use of the ThreadPool and the Task class for efficient thread handling and value returns.

Uploaded by

Dharna Ahuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Multithreading in C#

What is Multithreading?
• Multithreading allows concurrent execution of
code.
• Threads are lightweight processes.
• Improves efficiency, especially in multi-core
CPUs.
Thread Class in C#
• Example using Thread class:
• using System.Threading;
void PrintNumbers() {
for (int i = 0; i < 5; i++) {
Console.WriteLine(i);
}
}
Thread t = new Thread(PrintNumbers);
t.Start();
Thread Lifecycle
• Thread lifecycle includes Unstarted → Running
→ WaitSleepJoin → Stopped.
• Thread t = new Thread(() =>
Thread.Sleep(1000));
Console.WriteLine(t.ThreadState); // Unstarted
t.Start();
Console.WriteLine(t.ThreadState); // Running
Thread Synchronization
• Use lock to avoid race conditions:
• object lockObj = new object();
int counter = 0;
void Increment() {
lock(lockObj) {
counter++;
}
}
Thread Safety
• Use locking or thread-safe collections:
• ConcurrentDictionary<int, string> dict = new();
dict.TryAdd(1, "value");
ThreadPool in C#
• Efficient management of threads using
ThreadPool:
• ThreadPool.QueueUserWorkItem(state => {
Console.WriteLine("Running in thread
pool.");
});
Tasks vs Threads
• Tasks provide simpler and more flexible thread
handling:
• Task task = Task.Run(() => {
Console.WriteLine("Task running...");
});
task.Wait();
Async and Await - Deep Explanation
• Async methods allow non-blocking operations.
Await pauses execution until result is ready.
• async Task DownloadFileAsync() {
HttpClient client = new HttpClient();
string content = await
client.GetStringAsync("http://example.com");
Console.WriteLine(content);
}
Best Practices in Multithreading
• Use async/await for I/O, avoid blocking. Use
ConfigureAwait(false) in libraries.
• async Task SaveAsync() {
await File.WriteAllTextAsync("file.txt",
"data").ConfigureAwait(false);
}
Thread Synchronization - Race Condition Fix
• Example showing proper synchronization with multiple threads:
• int counter = 0;
object lockObj = new object();

void Increment() {
for (int i = 0; i < 1000; i++) {
lock (lockObj) {
counter++;
}
}
}

Thread t1 = new Thread(Increment);


Thread t2 = new Thread(Increment);
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine("Counter: " + counter);
Task Class - Continuation
• Tasks can be chained using ContinueWith:
• Task task1 = Task.Run(() => {
Console.WriteLine("Task 1 running");
});

Task task2 = task1.ContinueWith(t => {


Console.WriteLine("Task 2 running after Task 1");
});

task2.Wait();
Task Class - Returning Values
• Tasks can return values using generics:
• Task<int> computeTask = Task.Run(() => {
return 42;
});

int result = computeTask.Result;


Console.WriteLine("Result: " + result);
Thread vs Task - Code Comparison
• Threads are low-level, Tasks are high-level and
managed:
• // Using Thread
Thread thread = new Thread(() =>
Console.WriteLine("Thread running"));
thread.Start();

// Using Task
Task task = Task.Run(() => Console.WriteLine("Task
running"));
task.Wait();

You might also like