Book Image

Multithreading in C# 5.0 Cookbook

By : Evgenii Agafonov
Book Image

Multithreading in C# 5.0 Cookbook

By: Evgenii Agafonov

Overview of this book

In an age when computer processors are being developed to contain more and more cores, multithreading is a key factor for creating scalable, effective, and responsive applications. If you fail to do it correctly, it can lead to puzzling problems that take a huge amount of time to resolve. Therefore, having a solid understanding of multithreading is a must for the modern application developer. Multithreading in C# 5.0 Cookbook is an easy-to-understand guide to the most puzzling programming problems. This book will guide you through practical examples dedicated to various aspects of multithreading in C# on Windows and will give you a good basis of practical knowledge which you can then use to program your own scalable and reliable multithreaded applications. This book guides you through asynchronous and parallel programming from basic examples to practical, real-world solutions to complex problems. You will start from the very beginning, learning what a thread is, and then proceed to learn new concepts based on the information you get from the previous examples. After describing the basics of threading, you will be able to grasp more advanced concepts like Task Parallel Library and C# asynchronous functions. Then, we move towards parallel programming, starting with basic data structures and gradually progressing to the more advanced patterns. The book concludes with a discussion of the specifics of Windows 8 application programming, giving you a complete understanding of how Windows 8 applications are different and how to program asynchronous applications for Windows 8.
Table of Contents (18 chapters)
Multithreading in C# 5.0 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Not so long ago, a typical personal computer CPU had only one computing core, and the power consumption was enough to cook fried eggs on it. In 2005, Intel introduced its first multiple-core CPU, and since then computers started developing in a different direction. Low power consumption and a number of computing cores became more important than a row computing core performance. This lead to programming paradigm changes as well. Now we need to learn how to use all CPU cores effectively to achieve the best performance, and at the same time, save battery power by running only the programs we need at a particular time. Besides that, we need to program server applications in a way to use multiple CPU cores or even multiple computers as efficiently as possible to support as many users as we can.

To be able to create such applications, you have to learn to use multiple CPU cores in your programs effectively. If you use the Microsoft .NET development platform and C# programming language, this book will be a perfect starting point for programming applications that have good performance and responsiveness.

The purpose of this book is to provide you with a step-by-step guide for multithreading and parallel programming in C#. We will start with the basic concepts, going through more and more advanced topic based on the information from previous chapters, and end with real-world parallel programming patterns and Windows Store application samples.

What this book covers

Chapter 1, Threading Basics, introduces basic operations with threads in C#. It explains what a thread is, the pros and cons of using threads, and other important thread aspects.

Chapter 2, Thread Synchronization, describes thread interaction details. You will learn why we need to coordinate threads together and the different ways of organizing thread coordination.

Chapter 3, Using a Thread Pool, explains a thread pool concept. It shows how to use a thread pool, how to work with asynchronous operations, and good and bad practices of using a thread pool.

Chapter 4, Using Task Parallel Library, is a deep dive into a Task Parallel Library framework. This chapter outlines every important aspect of TPL, including tasks combination, exceptions management, and operations cancellation.

Chapter 5, Using C# 5.0, explains in detail the new C# 5.0 feature – asynchronous methods. You will find out what async and await keywords mean, how to use them in different scenarios, and how await works under the hood.

Chapter 6, Using Concurrent Collections, describes standard data structures for parallel algorithms included in the .NET Framework. It goes through sample programming scenarios for each data structure.

Chapter 7, Using PLINQ, is a deep dive into the Parallel LINQ infrastructure. The chapter describes task and data parallelism, parallelizing a LINQ query, tweaking parallelism options, partitioning a query, and aggregating parallel query result.

Chapter 8, Reactive Extensions, explains how and when to use the Reactive Extensions framework. You will learn how to compose events and how to perform a LINQ query against an event sequence.

Chapter 9, Using Asynchronous I/O, covers in detail the asynchronous I/O process including files, networks, and database scenarios.

Chapter 10, Parallel Programming Patterns, outlines the common parallel programming problem solutions.

Chapter 11, There's More, covers the aspects of programming asynchronous applications for Windows 8. You will learn how to work with Windows 8 asynchronous APIs, and how to perform background work in Windows Store applications.

What you need for this book

For most of the recipes, you will need Microsoft Visual Studio Express 2012 for Windows Desktop. Recipes in Chapter 11 will require Windows 8 and Microsoft Visual Studio Express 2012 for Windows 8 to compile Windows Store applications.

Who this book is for

Multithreading in C# 5.0 Cookbook is written for existing C# developers with little or no background in multithreading, and asynchronous and parallel programming. The book covers these topics from basic concepts to complicated programming patterns and algorithms using C# and .NET ecosystem.

Conventions

In this book, you will find a number of styles of text that distinguish among different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: " When we construct a thread, an instance of the ThreadStart or ParameterizedThreadStart delegate is passed to the constructor."

A block of code is set as follows:

static void PrintNumbers()
{
  Console.WriteLine("Starting...");
  for (int i = 1; i < 10; i++)
  {
    Console.WriteLine(i);
  }
}

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Start Visual Studio 2012. Create a new C# Console Application project."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic in which you have expertise, and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.