Parallel Programming and Concurrency with C# 10 and .NET 6 - Alvin Ashcraft - E-Book

Parallel Programming and Concurrency with C# 10 and .NET 6 E-Book

Alvin Ashcraft

0,0
29,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

.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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 314

Veröffentlichungsjahr: 2022

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Parallel Programming and Concurrency with C# 10 and .NET 6

A modern approach to building faster, more responsive, and asynchronous .NET applications using C#

Alvin Ashcraft

BIRMINGHAM—MUMBAI

Parallel Programming and Concurrency with C# 10 
and .NET 6

Copyright © 2022 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Associate Group Product Manager: Gebin George

Publishing Product Manager: Sathyanarayanan Ellapulli

Senior Editor: Ruvika Rao

Content Development Editor: Yashi Gupta

Technical Editor: Maran Fernandes

Copy Editor: Safis Editing

Project Coordinator: Manisha Singh

Proofreader: Safis Editing

Indexer: Hemangini Bari

Production Designer: Roshan Kawale

Marketing Coordinator: Sonakshi Bubbar

First published: August 2022

Production reference: 1120822

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-80324-367-2

www.packt.com

To my wife, Stelene, for supporting me through the process of writing a second book and for helping me become my best self in our journey together. To my three daughters, for working harder and smarter than I did at their ages and for their patience during my writing process.

– Alvin Ashcraft

Contributors

About the author

Alvin Ashcraft is a writer, software engineer, and developer community champion with over 27 years of experience in software and content development. He has worked with Microsoft Windows, web, and cloud technologies since 1995 and has been awarded as a Microsoft MVP 13 times.

Alvin works remotely in the Philadelphia area for Microsoft as a content developer on Microsoft Learn for the Windows developer documentation team. He also helps organize the TechBash developer conference in the Northeast US. He has previously worked for companies such as Oracle, Genzeon, and Allscripts. Originally from the Allentown, PA, area, Alvin currently resides in West Grove, PA, with his wife and daughters.

I want to thank the people who have been close to me and supported me, especially my parents and my wife, Stelene’s, parents.

About the reviewers

Ricardo Peres is a Portuguese developer, blogger, and book author and is currently a team leader at Dixons Carphone. He has over 20 years of experience in software development and his interests include distributed systems, architectures, design patterns, and .NET development. He won the Microsoft MVP award in 2015 and held this title up to 2020.

He also authored Entity Framework Core Cookbook – Second Edition and Mastering ASP.NET Core 2.0 and was a technical reviewer for Learning NHibernate 4 for Packt. He also contributed to Syncfusion’s Succinctly collection, with titles on .NET development. Ricardo maintains a blog—Development With A Dot—where he writes about technical issues. You can keep up with him on Twitter at @rjperes75.

Joseph Guadagno is a senior director of engineering at Rocket Mortgage, the US’s largest mortgage lender, based in Detroit, Michigan. He has been writing software for over 30 years and has been an active member of the .NET community, serving as a Microsoft MVP in .NET, for more than 10 years. He has spoken throughout the United States and at international events on topics including Microsoft .NET, Microsoft Azure, and SQL. You can see the complete list of events he has spoken at at https://www.josephguadagno.net/presentations.

When not sitting at a computer, Joe loves to hang out with his family and play games. You can connect with Joe on Twitter at @jguadagno, on Facebook at JosephGuadagnoNet, and on his blog at https://www.josephguadagno.net. 

Table of Contents

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

Part 1:Introduction to Threading in .NET

In this part, you will learn the basics of managed threading in .NET, discover how it has evolved since the early days of the .NET Framework, and pick up some best practices to avoid common pitfalls.

This part contains the following chapters:

Chapter 1, Managed Threading ConceptsChapter 2, Evolution of Multithreaded Programming in .NETChapter 3, Best Practices for Managed ThreadingChapter 4, User Interface Responsiveness with Threading