Parallel Programming and Concurrency with C# 10 and .NET 6: A modern approach to building faster, more responsive, and asynchronous .NET applications using C#
- Length: 320 pages
- Edition: 1
- Language: English
- Publisher: Packt Publishing
- Publication Date: 2022-08-30
- ISBN-10: 1803243678
- ISBN-13: 9781803243672
- Sales Rank: #612174 (See Top 100 Books)
Leverage the latest parallel and concurrency features in .NET 6 when building your next application and explore the benefits and challenges of asynchrony, parallelism, and concurrency in .NET via practical examples
Key Features
- Learn to implement parallel programming and handle concurrency in .NET efficiently
- Switch threads while debugging and learn how to monitor specific threads in Visual Studio
- Discover how to cancel tasks with callbacks, by polling, or by using a task with wait handles
Book Description
.NET has included managed threading capabilities since the beginning, but early techniques had inherent risks: memory leaks, thread synchronization issues, and deadlocks. This book will help you avoid those pitfalls and leverage the modern constructs available in .NET 6 and C# 10, while providing recommendations on patterns and best practices for parallelism and concurrency. Parallel, concurrent, and asynchronous programming are part of every .NET application today, and it becomes imperative for modern developers to understand how to effectively use these techniques.
This book will teach intermediate-level .NET developers how to make their applications faster and more responsive with parallel programming and concurrency in .NET and C# with practical examples. The book starts with the essentials of multi-threaded .NET development and explores how the language and framework constructs have evolved along with .NET. You will later get to grips with the different options available today in .NET 6, followed by insights into best practices, debugging, and unit testing.
By the end of this book, you will have a deep understanding of why, when, and how to employ parallelism and concurrency in any .NET application.
What you will learn
- Prevent deadlocks and race conditions with managed threading
- Update Windows app UIs without causing exceptions
- Explore best practices for introducing asynchronous constructs to existing code
- Avoid pitfalls when introducing parallelism to your code
- Implement the producer-consumer pattern with Dataflow blocks
- Enforce data sorting when processing data in parallel and safely merge data from multiple sources
- Use concurrent collections that help synchronize data across threads
- Debug an everyday parallel app with the Parallel Stacks and Parallel Tasks windows
Who this book is for
This book is for beginner to intermediate-level .NET developers who want to employ the latest parallel and concurrency features in .NET when building their applications. Readers should have a solid understanding of the C# language and any version of the .NET Framework or .NET Core.
Parallel Programming and Concurrency with C# 10 and .NET 6 Contributors About the author About the reviewers Preface Who this book is for What this book covers To get the most out of this book Download the example code files Download the color images Conventions used Get in touch Share Your Thoughts Part 1:Introduction to Threading in .NET Chapter 1: Managed Threading Concepts Technical requirements .NET threading basics Threads and processes When should we use multithreading in .NET? Background threads What is managed threading? Creating and destroying threads Creating managed threads Pausing thread execution Destroying managed threads Handling threading exceptions Synchronizing data across threads Synchronizing code regions Manual synchronization Scheduling and canceling work Scheduling managed threads Canceling managed threads Summary Questions Chapter 2: Evolution of Multithreaded Programming in .NET Technical requirements .NET threading through the years C# 4 and .NET Framework 4.0 C# 5 and 6 and .NET Framework 4.5.x C# 7.x and .NET Core 2.0 C# 8 and .NET Core 3.0 C# 10 and .NET 6 Beyond threading basics Managed thread pool Threading and timers Introduction to parallelism Using Parallel.Invoke Using Parallel.ForEach Basics of Parallel LINQ Introduction to concurrency ConcurrentBag<T> ConcurrentQueue<T> ConcurrentStack<T> BlockingCollection<T> ConcurrentDictionary<TKey, TValue> Basics of async and await Understanding the async keyword Writing an async method Choosing the right path forward Summary Questions Chapter 3: Best Practices for Managed Threading Technical requirements Handling static objects Static data and constructors Controlling shared access to static objects Managing deadlocks and race conditions Mitigating deadlocks Avoiding race conditions Threading limits and other recommendations Summary Questions Chapter 4: User Interface Responsiveness and Threading Technical requirements Leveraging background threads Which threads are background threads? Using async, await, tasks, and WhenAll Using the thread pool Updating the UI thread without exceptions Summary Questions Part 2: Parallel Programming and Concurrency with C# Chapter 5: Asynchronous Programming with C# Technical requirements More about asynchronous programming in .NET I/O-bound operations CPU-bound operations Nested async methods Working with Task objects Exploring Task methods Exploring Task properties Interop with synchronous code Executing async from synchronous methods Executing synchronous code as async Working with multiple background tasks Asynchronous programming best practices Summary Questions Chapter 6: Parallel Programming Concepts Technical requirements Getting started with the TPL I/O-bound operations CPU-bound operations Parallel loops in .NET Basic Parallel.For loops Parallel loops with thread-local variables Simple Parallel.ForEach loops Cancel a Parallel.ForEachAsync loop Relationships between parallel tasks Under the covers of Parallel.Invoke Understanding parallel child tasks Common pitfalls with parallelism Parallelism is not guaranteed Parallel loops are not always faster Beware of blocking the UI thread Thread safety UI controls ThreadLocal data Summary Questions Chapter 7: Task Parallel Library (TPL) and Dataflow Technical requirements Introducing the TPL Dataflow library Why use the TPL Dataflow library? Types of dataflow blocks Implementing the producer/consumer pattern Creating a data pipeline with multiple blocks Manipulating data from multiple data sources Summary Questions Chapter 8: Parallel Data Structures and Parallel LINQ Technical requirements Introducing PLINQ PLINQ and performance Creating a PLINQ query Query syntax versus method syntax Converting LINQ queries to PLINQ Handling exceptions with PLINQ queries Preserving data order and merging data with PLINQ PLINQ data order samples Using WithMergeOptions in PLINQ queries Data structures for parallel programming in .NET Concurrent collections Synchronization primitives Summary Questions Chapter 9: Working with Concurrent Collections in .NET Technical requirements Using BlockingCollection BlockingCollection details Using BlockingCollection with Parallel.ForEach and PLINQ Using ConcurrentBag Using ConcurrentDictionary Using ConcurrentQueue Using ConcurrentStack Summary Questions Part 3: Advanced Concurrency Concepts Chapter 10: Debugging Multithreaded Applications with Visual Studio Technical requirements Introducing multithreaded debugging Debugging threads and processes Debugging a project with multiple threads Exploring the Threads window Switching and flagging threads Switching threads Flagging threads Freezing threads Debugging a parallel application Using the Parallel Stacks window Using the Parallel Watch window Summary Questions Chapter 11: Canceling Asynchronous Work Technical requirements Canceling managed threads Canceling parallel work Canceling a parallel loop Canceling a PLINQ query Discovering patterns for thread cancellation Canceling with polling Canceling with callbacks Canceling with wait handles Handling multiple cancellation sources Summary Questions Chapter 12: Unit Testing Async, Concurrent, and Parallel Code Technical requirements Unit testing asynchronous code Unit testing concurrent code Unit testing parallel code Checking for memory leaks with unit tests Summary Questions Assessments Chapter 1, Managed Threading Concepts Chapter 2, Evolution of Multithreaded Programming in .NET Chapter 3, Best Practices for Managed Threading Chapter 4, User Interface Responsiveness and Threading Chapter 5, Asynchronous Programming with C# Chapter 6, Parallel Programming Concepts Chapter 7, Task Parallel Library (TPL) and Dataflow Chapter 8, Parallel Data Structures and Parallel LINQ Chapter 9, Working with Concurrent Collections in .NET Chapter 10, Debugging Multithreaded Applications with Visual Studio Chapter 11, Canceling Asynchronous Work Chapter 12, Unit Testing Async, Concurrent, and Parallel Code Why subscribe? Other Books You May Enjoy Packt is searching for authors like you Share Your Thoughts
Donate to keep this site alive
How to download source code?
1. Go to: https://github.com/PacktPublishing
2. In the Find a repository… box, search the book title: Parallel Programming and Concurrency with C# 10 and .NET 6: A modern approach to building faster, more responsive, and asynchronous .NET applications using C#
, sometime you may not get the results, please search the main title.
3. Click the book title in the search results.
3. Click Code to download.
1. Disable the AdBlock plugin. Otherwise, you may not get any links.
2. Solve the CAPTCHA.
3. Click download link.
4. Lead to download server to download.