C# Concurrency

This C# concurrency series helps you master multithreading and asynchronous programming in C# and shows you how to use C# concurrency to improve the application’s performance.

What you’ll learn

  • Understand the basic concepts including threads, processes, and the difference between threads and processes.
  • Fully Utilize the power of the task parallel library (TPL) using Task objects.
  • Deeply understand how async/await works in C#.

Section 1. Multithreading

This section helps you understand the basic concepts of thread and multithreading in C# and build a strong foundation for developing asynchronous applications.

  • Thread – understand the Thread object and how to create a multithreading application in C#.
  • Background thread – understand background threads and the differences between background & foreground threads.
  • ThreadPool – learn how to use the thread pool to manage threads more efficiently.

Section 2. Task-based Asynchronous Programming (TAP)

The task-based asynchronous programming (TAP) is the modern programming model in C# that simplifies the process of writing asynchronous code.

  • Task – explain the task-based asynchronous programming model and show you how to use the Task class to create asynchronous operations that can be executed concurrently.
  • Chain multiple tasks – show you how to use the ContinueWith() method of the Task class to chain multiple tasks.
  • Handle exceptions – learn how to handle AggregateException exceptions that are thrown by asynchronous operations executed by tasks.
  • Async/Await – show you how to use the async/await keywords to create methods that execute asynchronously.
  • Canceling an asynchronous operation – learn how to cancel an asynchronous operation using the cancellation token.
  • Task.WhenAny() – wait for the first task in a collection of tasks to complete and process the result of that task.
  • Task.WhenAll() – wait for all tasks in a collection of tasks to complete.

Section 3. Thread Synchronization

This section shows you how to coordinate the execution of multiple threads to ensure that they access the shared resources in a thread-safe manner and prevent race conditions as well as other synchronization issues.

  • lock statement – show you how to use the lock statement to prevent the risk of race conditions and synchronization when multiple tasks (or threads) attempt to access the same shared resources.
  • Deadlock – understand what is a deadlock, how it occurs, and how to fix it using various techniques.
  • Interlocked – learn how to perform atomic operations on a shared variable using methods of the Interlocked class.
  • ReaderWriterLockSlim – guide you on how to use the ReaderWriterLockSlim class to allow multiple threads to read from a shared resource simultaneously while preventing other threads from concurrent writes.
  • SemaphoreSlim – learn how to use the semaphore object to limit the number of threads that access shared resources concurrently.

Section 4. Thread Signaling

This section shows you how to communicate between threads via signals using signaling objects including AutoResetEvent, ManualResetEventSlim, and CountDownEvent.

  • AutoResetEvent – learn how to send signals between threads using the AutoResetEvent object.
  • ManualResetEvent – show you how to use the ManualResetEventSlim class to signal events between threads.
  • CountdownEvent – guide you on how to use the CountdownEvent object to wait for a specified number of events before continuing execution on waiting threads.