Multithreading with C# Cookbook - Second Edition

Over 70 recipes to get you writing powerful and efficient multithreaded, asynchronous, and parallel programs in C# 6.0
Preview in Mapt

Multithreading with C# Cookbook - Second Edition

Eugene Agafonov

3 customer reviews
Over 70 recipes to get you writing powerful and efficient multithreaded, asynchronous, and parallel programs in C# 6.0
Mapt Subscription
FREE
$29.99/m after trial
eBook
$20.00
RRP $39.99
Save 49%
Print + eBook
$49.99
RRP $49.99
What do I get with a Mapt Pro subscription?
  • Unlimited access to all Packt’s 5,000+ eBooks and Videos
  • Early Access content, Progress Tracking, and Assessments
  • 1 Free eBook or Video to download and keep every month after trial
What do I get with an eBook?
  • Download this book in EPUB, PDF, MOBI formats
  • DRM FREE - read and interact with your content when you want, where you want, and how you want
  • Access this title in the Mapt reader
What do I get with Print & eBook?
  • Get a paperback copy of the book delivered to you
  • Download this book in EPUB, PDF, MOBI formats
  • DRM FREE - read and interact with your content when you want, where you want, and how you want
  • Access this title in the Mapt reader
What do I get with a Video?
  • Download this Video course in MP4 format
  • DRM FREE - read and interact with your content when you want, where you want, and how you want
  • Access this title in the Mapt reader
$0.00
$20.00
$49.99
$29.99 p/m after trial
RRP $39.99
RRP $49.99
Subscription
eBook
Print + eBook
Start 14 Day Trial

Frequently bought together


Multithreading with C# Cookbook - Second Edition Book Cover
Multithreading with C# Cookbook - Second Edition
$ 39.99
$ 20.00
Mastering C# and .NET Framework Book Cover
Mastering C# and .NET Framework
$ 35.99
$ 18.00
Buy 2 for $35.00
Save $40.98
Add to Cart

Book Details

ISBN 139781785881251
Paperback264 pages

Book Description

Multi-core processors are synonymous with computing speed and power in today’s world, which is why multithreading has become a key concern for C# developers. Multithreaded code helps you create effective, scalable, and responsive applications.

This is an easy-to-follow guide that will show you difficult programming problems in context. You will learn how to solve them with practical, hands-on, recipes. With these recipes, you’ll be able to start creating your own scalable and reliable multithreaded applications. Starting from learning what a thread is, we guide you through the basics and then move on to more advanced concepts such as task parallel libraries, C# asynchronous functions, and much more.

Rewritten to the latest C# specification, C# 6, and updated with new and modern recipes to help you make the most of the hardware you have available, this book will help you push the boundaries of what you thought possible in C#.

Table of Contents

Chapter 1: Threading Basics
Introduction
Creating a thread in C#
Pausing a thread
Making a thread wait
Aborting a thread
Determining a thread state
Thread priority
Foreground and background threads
Passing parameters to a thread
Locking with a C# lock keyword
Locking with a Monitor construct
Handling exceptions
Chapter 2: Thread Synchronization
Introduction
Performing basic atomic operations
Using the Mutex construct
Using the SemaphoreSlim construct
Using the AutoResetEvent construct
Using the ManualResetEventSlim construct
Using the CountDownEvent construct
Using the Barrier construct
Using the ReaderWriterLockSlim construct
Using the SpinWait construct
Chapter 3: Using a Thread Pool
Introduction
Invoking a delegate on a thread pool
Posting an asynchronous operation on a thread pool
A thread pool and the degree of parallelism
Implementing a cancellation option
Using a wait handle and timeout with a thread pool
Using a timer
Using the BackgroundWorker component
Chapter 4: Using the Task Parallel Library
Introduction
Creating a task
Performing basic operations with a task
Combining tasks
Converting the APM pattern to tasks
Converting the EAP pattern to tasks
Implementing a cancelation option
Handling exceptions in tasks
Running tasks in parallel
Tweaking the execution of tasks with TaskScheduler
Chapter 5: Using C# 6.0
Introduction
Using the await operator to get asynchronous task results
Using the await operator in a lambda expression
Using the await operator with consequent asynchronous tasks
Using the await operator for the execution of parallel asynchronous tasks
Handling exceptions in asynchronous operations
Avoiding the use of the captured synchronization context
Working around the async void method
Designing a custom awaitable type
Using the dynamic type with await
Chapter 6: Using Concurrent Collections
Introduction
Using ConcurrentDictionary
Implementing asynchronous processing using ConcurrentQueue
Changing asynchronous processing order with ConcurrentStack
Creating a scalable crawler with ConcurrentBag
Generalizing asynchronous processing with BlockingCollection
Chapter 7: Using PLINQ
Introduction
Using the Parallel class
Parallelizing a LINQ query
Tweaking the parameters of a PLINQ query
Handling exceptions in a PLINQ query
Managing data partitioning in a PLINQ query
Creating a custom aggregator for a PLINQ query
Chapter 8: Reactive Extensions
Introduction
Converting a collection to an asynchronous Observable
Writing custom Observable
Using the Subject type family
Creating an Observable object
Using LINQ queries against an observable collection
Creating asynchronous operations with Rx
Chapter 9: Using Asynchronous I/O
Introduction
Working with files asynchronously
Writing an asynchronous HTTP server and client
Working with a database asynchronously
Calling a WCF service asynchronously
Chapter 10: Parallel Programming Patterns
Introduction
Implementing Lazy-evaluated shared states
Implementing Parallel Pipeline with BlockingCollection
Implementing Parallel Pipeline with TPL DataFlow
Implementing Map/Reduce with PLINQ
Chapter 11: There's More
Introduction
Using a timer in a Universal Windows Platform application
Using WinRT from usual applications
Using BackgroundTask in Universal Windows Platform applications
Running a .NET Core application on OS X
Running a .NET Core application on Ubuntu Linux

What You Will Learn

  • Use C# 6.0 asynchronous language features
  • Work with raw threads, synchronize threads, and coordinate their work
  • Develop your own asynchronous API with Task Parallel Library
  • Work effectively with a thread pool
  • Scale up your server application with I/O threads
  • Parallelize your LINQ queries with PLINQ
  • Use common concurrent collections
  • Apply different parallel programming patterns
  • Use Reactive Extensions to run asynchronous operations and manage their options

Authors

Table of Contents

Chapter 1: Threading Basics
Introduction
Creating a thread in C#
Pausing a thread
Making a thread wait
Aborting a thread
Determining a thread state
Thread priority
Foreground and background threads
Passing parameters to a thread
Locking with a C# lock keyword
Locking with a Monitor construct
Handling exceptions
Chapter 2: Thread Synchronization
Introduction
Performing basic atomic operations
Using the Mutex construct
Using the SemaphoreSlim construct
Using the AutoResetEvent construct
Using the ManualResetEventSlim construct
Using the CountDownEvent construct
Using the Barrier construct
Using the ReaderWriterLockSlim construct
Using the SpinWait construct
Chapter 3: Using a Thread Pool
Introduction
Invoking a delegate on a thread pool
Posting an asynchronous operation on a thread pool
A thread pool and the degree of parallelism
Implementing a cancellation option
Using a wait handle and timeout with a thread pool
Using a timer
Using the BackgroundWorker component
Chapter 4: Using the Task Parallel Library
Introduction
Creating a task
Performing basic operations with a task
Combining tasks
Converting the APM pattern to tasks
Converting the EAP pattern to tasks
Implementing a cancelation option
Handling exceptions in tasks
Running tasks in parallel
Tweaking the execution of tasks with TaskScheduler
Chapter 5: Using C# 6.0
Introduction
Using the await operator to get asynchronous task results
Using the await operator in a lambda expression
Using the await operator with consequent asynchronous tasks
Using the await operator for the execution of parallel asynchronous tasks
Handling exceptions in asynchronous operations
Avoiding the use of the captured synchronization context
Working around the async void method
Designing a custom awaitable type
Using the dynamic type with await
Chapter 6: Using Concurrent Collections
Introduction
Using ConcurrentDictionary
Implementing asynchronous processing using ConcurrentQueue
Changing asynchronous processing order with ConcurrentStack
Creating a scalable crawler with ConcurrentBag
Generalizing asynchronous processing with BlockingCollection
Chapter 7: Using PLINQ
Introduction
Using the Parallel class
Parallelizing a LINQ query
Tweaking the parameters of a PLINQ query
Handling exceptions in a PLINQ query
Managing data partitioning in a PLINQ query
Creating a custom aggregator for a PLINQ query
Chapter 8: Reactive Extensions
Introduction
Converting a collection to an asynchronous Observable
Writing custom Observable
Using the Subject type family
Creating an Observable object
Using LINQ queries against an observable collection
Creating asynchronous operations with Rx
Chapter 9: Using Asynchronous I/O
Introduction
Working with files asynchronously
Writing an asynchronous HTTP server and client
Working with a database asynchronously
Calling a WCF service asynchronously
Chapter 10: Parallel Programming Patterns
Introduction
Implementing Lazy-evaluated shared states
Implementing Parallel Pipeline with BlockingCollection
Implementing Parallel Pipeline with TPL DataFlow
Implementing Map/Reduce with PLINQ
Chapter 11: There's More
Introduction
Using a timer in a Universal Windows Platform application
Using WinRT from usual applications
Using BackgroundTask in Universal Windows Platform applications
Running a .NET Core application on OS X
Running a .NET Core application on Ubuntu Linux

Book Details

ISBN 139781785881251
Paperback264 pages
Read More
From 3 reviews

Read More Reviews

Recommended for You

Mastering C# and .NET Framework Book Cover
Mastering C# and .NET Framework
$ 35.99
$ 18.00
.NET Design Patterns Book Cover
.NET Design Patterns
$ 31.99
$ 16.00
C# 7 and .NET Core Cookbook Book Cover
C# 7 and .NET Core Cookbook
$ 39.99
$ 20.00
C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition Book Cover
C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition
$ 39.99
$ 20.00
C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development - Third Edition Book Cover
C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development - Third Edition
$ 31.99
$ 16.00
C# Programming Cookbook Book Cover
C# Programming Cookbook
$ 39.99
$ 20.00