Reader small image

You're reading from  Parallel Programming and Concurrency with C# 10 and .NET 6

Product typeBook
Published inAug 2022
PublisherPackt
ISBN-139781803243672
Edition1st Edition
Right arrow
Author (1)
Alvin Ashcraft
Alvin Ashcraft
author image
Alvin Ashcraft

Alvin Ashcraft is a software engineer and developer community champion with over 25 years of experience in software development. Working primarily with Microsoft Windows, web, and cloud technologies, his career has focused primarily on the healthcare industry. He has been awarded as a Microsoft MVP 11 times, most recently as a Windows Dev MVP. Alvin works in the Philadelphia area for Allscripts, a global healthcare software company, as a principal software engineer. He is also a board member of the TechBash Foundation, where he helps organize the annual TechBash developer conference. He has previously worked for companies such as Oracle, Genzeon, CSC, and ITG Pathfinders. Originally from the Allentown, PA area, Alvin currently resides in West Grove, PA with his wife and three daughters.
Read more about Alvin Ashcraft

Right arrow

Preface

Parallel programming and concurrency have become prevalent in modern software development. In this book, you will learn how to leverage the latest asynchronous, parallel, and concurrency features in .NET 6 when building your next application. We will explore the power of multithreaded C# development patterns and practices. By exploring the benefits and challenges of threading in .NET through concise, real-world examples, choosing the right option for your project will become second nature.

You have many choices when introducing multithreading to a new or existing .NET application. The goal of this book is to not only teach you how to use parallel programming and concurrency in C# and .NET but also to help you understand which of the constructs to choose for a given scenario. Whether you are developing for desktop, mobile, the web, or the cloud, performance and responsiveness are key to the success of an application. This book will help every type of C# developer to scale their applications to their users’ needs and avoid the pitfalls often encountered with multithreaded development.

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. You should have a solid understanding of the C# language and some version of the .NET Framework or .NET Core.

What this book covers

Chapter 1, Managed Threading Concepts, covers the basics of working with managed threading in .NET. We will discuss how to create and destroy threads, handle exceptions, synchronize data, and the objects provided by .NET to handle background operations. You will gain a basic understanding of how threads can be managed in a .NET application. Practical examples in this chapter will illustrate how to use managed threading in C# projects.

Chapter 2, Evolution of Multithreaded Programming in .NET, introduces some of the concepts and features that will be explored in more depth in later chapters, including async/await, concurrent collections, and parallelism. You will learn how their options are expanded when selecting how to approach concurrency in applications.

Chapter 3, Best Practices for Managed Threading, covers some best practices when it comes to integrating managed threading concepts. We will cover important concepts such as static data, deadlocks, and exhausting managed resources. These are all areas that can lead to unstable applications and unexpected behavior. You will be given practical advice to avoid these pitfalls.

Chapter 4, User Interface Responsiveness with Threading, explains how to use ThreadPool in .NET. The real-world examples in this chapter will give you valuable options for ensuring UI responsiveness in your .NET applications.

Chapter 5, Asynchronous Programming with C#, explains asynchronous programming in C# and explores the best use of tasks in .NET.

Chapter 6, Parallel Programming Concepts, delves deeper into the Task Parallel Library (TPL) and tasking concepts.

Chapter 7, Task Parallel Library (TPL) and Dataflow, introduces the TPL Dataflow Library and illustrates some common patterns for its use through in-depth examples.

Chapter 8, Parallel Data Structures and Parallel LINQ (PLINQ), explores some of .NET’s useful features, including Parallel LINQ (PLINQ). Follow along with some practical examples of PLINQ in C#.

Chapter 9, Working with Concurrent Collections in .NET, dives deeper into some of the concurrent collections that help provide data integrity when using concurrency and parallelism in your code.

Chapter 10, Debugging Multithreaded Applications with Visual Studio, teaches you how to use the power of Visual Studio when debugging multithreaded .NET applications. This chapter will explore the tools in detail through concrete examples.

Chapter 11, Canceling Asynchronous Work, dives deeper into the different methods available to cancel concurrent and parallel work with .NET. You will gain a deep understanding of how to safely cancel asynchronous work.

Chapter 12, Unit Testing Async, Concurrent, and Parallel Code, provides some concrete advice and real-world examples of how developers can unit test code that employs multithreaded constructs. These examples will illustrate how unit tests can still be reliable while covering code that performs multithreaded operations.

To get the most out of this book

To follow along with the examples in this book, the following software is recommended for Windows developers:

  • Visual Studio 2022 version 17.0 or later
  • .NET 6

While these are recommended, if you have the .NET 6 SDK installed, you can use your preferred editor for most of the examples. For example, Visual Studio 2022 for Mac on macOS 10.13 or later, JetBrains Rider, or Visual Studio Code will work just as well. However, for any WPF or WinForms projects, Visual Studio and Windows are required. Newer versions of Visual Studio and .NET, when they are released, should also work with the examples in this book.

You are expected to have a foundational knowledge of C# and .NET with a working knowledge of Language Integrated Query (LINQ).

The most recent Visual Studio 2022 install instructions and prerequisites can always be found on Microsoft Docs here: https://docs.microsoft.com/visualstudio/install/install-visual-studio?view=vs-2022.

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

If you are unfamiliar with LINQ, there is a great C# reference on Microsoft Docs to get you started before working through the examples in this book: https://docs.microsoft.com/dotnet/csharp/programming-guide/concepts/linq/.

After reading this book, I would also recommend exploring the posts on the .NET Parallel Programming team blog. Most of the articles are several years old, but they explore the thinking behind many of the decisions made when building the .NET libraries that expose parallel programming constructs: https://devblogs.microsoft.com/pfxteam/.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Parallel-Programming-and-Concurrency-with-C-sharp-10-and-.NET-6. If there’s an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots and diagrams used in this book. You can download it here: https://packt.link/Z4GcQ.

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in the text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “By calling ThreadPool.SetMaxThreads, you can change the maximum values for workerThreads and completionPortThreads.”

A block of code is set as follows:

public async Task PerformCalculations()
{
    _runningTotal = 3;
    await MultiplyValue().ContinueWith(async (Task) => {
        await AddValue();
        });
    Console.WriteLine($”Running total is {_runningTotal}”);
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    private async Task MultiplyValue()
    {
        await Task.Delay(100);
        var currentTotal = Interlocked.Read(ref 
            _runningTotal);
        Interlocked.Exchange(ref _runningTotal, 
            currentTotal * 10);
    }
}

Any command-line input or output is written as follows:

$ mkdir css
$ cd css

Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “Let’s look at a quick example of how to implement this in our CancellationPatterns project.”

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, email us at customercare@packtpub.com and mention the book title in the subject of your message.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Share Your Thoughts

Once you’ve read Parallel Programming and Concurrency with C# 10 and .NET 6, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Parallel Programming and Concurrency with C# 10 and .NET 6
Published in: Aug 2022Publisher: PacktISBN-13: 9781803243672
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alvin Ashcraft

Alvin Ashcraft is a software engineer and developer community champion with over 25 years of experience in software development. Working primarily with Microsoft Windows, web, and cloud technologies, his career has focused primarily on the healthcare industry. He has been awarded as a Microsoft MVP 11 times, most recently as a Windows Dev MVP. Alvin works in the Philadelphia area for Allscripts, a global healthcare software company, as a principal software engineer. He is also a board member of the TechBash Foundation, where he helps organize the annual TechBash developer conference. He has previously worked for companies such as Oracle, Genzeon, CSC, and ITG Pathfinders. Originally from the Allentown, PA area, Alvin currently resides in West Grove, PA with his wife and three daughters.
Read more about Alvin Ashcraft