ENGLISH

Parallel Programming and Concurrency with C# 10 and .NET 6: A modern approach to building faster, more responsive, and asynchronous .NET applications using C#

Book information

Publisher
Packt Publishing
Year
2022
ISBN
1803243678, 9781803243672
Language
english
Format
PDF
Filesize
7 MB (7211740 bytes)
Pages
320\321
Time added
2022-09-19 13:30:20

Description

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 FeaturesLearn to implement parallel programming and handle concurrency in .NET efficientlySwitch threads while debugging and learn how to monitor specific threads in Visual StudioDiscover how to cancel tasks with callbacks, by polling, or by using a task with wait handlesBook 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 learnPrevent deadlocks and race conditions with managed threadingUpdate Windows app UIs without causing exceptionsExplore best practices for introducing asynchronous constructs to existing codeAvoid pitfalls when introducing parallelism to your codeImplement the producer-consumer pattern with Dataflow blocksEnforce data sorting when processing data in parallel and safely merge data from multiple sourcesUse concurrent collections that help synchronize data across threadsDebug an everyday parallel app with the Parallel Stacks and Parallel Tasks windowsWho 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. Table of ContentsManaged Threading ConceptsEvolution of Multi-Threaded Programming in .NETBest Practices for Managed ThreadingUser Interface Responsiveness with ThreadingAsynchronous Programming with C#Parallel Programming ConceptsTask Parallel Library (TPL) and DataflowParallel Data Structures and Parallel LINQWorking with Concurrent Collections in .NETDebugging Multi-Threaded Applications with Visual StudioCancelling Asynchronous WorkUnit Testing Async, Concurrent, and Parallel Code Cover Title Page Copyright and Credits Contributors Table of Contents Preface 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 ConcurrentQueue ConcurrentStack BlockingCollection ConcurrentDictionary 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 Index Other Books You May Enjoy

Similar books