Concurrency in C# Cookbook
284 pages
English
PDF
4.5

Concurrency in C# Cookbook

Concurrency in C# Cookbook

Author:

Year:

Summary:

Introduction

The book «Concurrency in C# Cookbook» by Stephen Cleary is one of the best practical guides to asynchronous and parallel programming in the .NET ecosystem. Published in 2019 (second edition), it is a collection of proven solutions for typical tasks that every C# developer working with multithreading, asynchrony, and parallelism encounters.

The book is practice-oriented: there is no excessive theory, but there are clear, ready-to-use recipes. Each recipe contains a problem statement, solution code, explanations, and warnings. This makes the book an indispensable reference for those who want to write efficient, safe, and scalable code in C#.

What This Book Is About

The book covers all key aspects of parallel programming in .NET, starting with the basics of async/await and ending with advanced topics such as reactive programming (Rx.NET), Channels, and testing asynchronous code. The book has 15 chapters, each dedicated to a specific area:

  • Chapter 1. Async/await Basics — creating and awaiting tasks, error handling, cancellation.
  • Chapter 2. Dataflow — the TPL Dataflow library for building data processing pipelines.
  • Chapter 3. Reactive Programming (Rx) — working with Observable sequences.
  • Chapter 4. Parallel Collections — ConcurrentDictionary, ConcurrentQueue, and other thread-safe structures.
  • Chapter 5. Locks and Synchronization — SemaphoreSlim, ReaderWriterLockSlim, AsyncLock.
  • Chapter 6. Channels — the new Producer/Consumer mechanism in .NET Core.
  • Chapter 7. Testing Asynchronous Code — mocks, timeouts, correctness checks.
  • Chapter 8. Advanced Scenarios — asynchronous streams (IAsyncEnumerable), ValueTask, ConfigureAwait.

Each recipe follows a consistent template: problem → solution → discussion → warnings. This allows you to quickly find the answer you need and immediately apply it in your project.

Who This Book Is For

The book is intended for C# developers who already have basic experience with the language and the .NET platform. It will be useful for:

  • Backend developers building high-load services on ASP.NET Core.
  • Desktop application developers (WPF, WinForms) who want to avoid UI freezes.
  • Engineers migrating from .NET Framework to .NET Core/.NET 5+ to master modern approaches to asynchrony.
  • Team leads and architects who need to make decisions about choosing tools for parallel processing.

Beginners in asynchronous programming should first read the introductory chapters, but overall the book assumes the reader is already familiar with the basics of C#.

What the Reader Will Learn

After studying the book, you will be able to:

  • Correctly use async/await without deadlocks and race conditions.
  • Build data processing pipelines using TPL Dataflow.
  • Work with reactive streams (Rx.NET) for processing events and streaming data.
  • Choose the appropriate thread-safe collection for a specific task.
  • Implement asynchronous locks and other synchronization primitives.
  • Use Channels for efficient communication between producer and consumer.
  • Write tests for asynchronous code using xUnit and Moq.
  • Understand the difference between Task, ValueTask, IAsyncEnumerable and choose the optimal type.

Strengths of the Book

Recommendations