Contents

About the Authors

About the Technical Reviewer

Acknowledgments

images Chapter 1: An Introduction to Asynchronous Programming

What Is Asynchronous Programming?

The Drive to Asynchrony

Mechanisms for Asynchrony

Multiple Machines

Multiple Processes

Multiple Threads

Thread Scheduling

Threads and Resources

Thread-Specific Resources

Resources Shared by Threads

Summary

images Chapter 2: The Evolution of the .NET Asynchronous API

Asynchrony in the World of .NET 1.0

System.Threading.Thread

Using the System Thread Pool

Changes to Async in .NET 1.1

Asynchrony in .NET 2.0

Logical and Physical Separation

Passing Data into a Thread

Closures

SynchronizationContext

Event-Based Asynchronous Pattern

Minor Changes in .NET 3.5

Lambda Expressions

Thread Pool Heuristics in .NET 3.5

Big Changes in .NET 4.0

Remodeling the Thread Pool Queue

Work-Stealing Queues

Thread Pool Heuristics in .NET 4.0

Summary

images Chapter 3: Tasks

What Is a Task?

Creating a Compute-Based Task

Passing Data into a Task

Dangers of Closures

Returning Data from a Task

Creating I/O-Based Tasks

Error Handling

Ignoring Errors

Designing Task-Based APIs

Cancellation

Progress

Task Relationships

Chaining Tasks (Continuations)

Nested and Child Tasks

Conclusion

images Chapter 4: Basic Thread Safety

Asynchrony and Data

It’s Not Always Good to Share

Immutable State

Atomic State Transition

Nonatomic State Transition

Correctness Is Not the Only Problem

Thread Safety

The Interlocked Class

Basic Operations

Richer Functions

Monitor: The Workhorse of .NET Synchronization

The lock Keyword

Timing Out of Monitor Acquisition

Signaling with Monitors

Signaling As a Building Block

Optimizing for Read

ReaderWriterLock

ReaderWriterLockSlim

A Semaphore Out of the Box

Raising the Starting Gate: ManualResetEventSlim

CountdownEvent: Simplifying Fork and Join

Barrier: Rendezvous-Based Synchronization

Crossing the AppDomain Boundary with WaitHandle

Mutex

Semaphore

Events

WaitHandle—The Kernel Synchronization Abstraction

Working with Multiple WaitHandles

Integrating Standard Primitives and Kernel Objects

Synchronization Is Not the Only Answer

Conclusion

images Chapter 5: Concurrent Data Structures

Simplifying Thread Safety

Lazy<T>

Concurrent Collections

ConcurrentDictionary<K,V>

Locking Mechanics

ConcurrentQueue<T> and ConcurrentStack<T>

ConcurrentBag<T>

Blocking Collections

Graceful Shutdown

Consuming Enumerable

BlockingCollection of X

Summary

images Chapter 6: Asynchronous UI

UI Mechanics

UI Threading Model

Synchronization Context

Send and Post

Task Continuations

Event-Based Asynchronous Pattern (EAP)

Background Worker

Data Binding

Windows Forms

Windows Presentation Foundation (WPF)

WinRT

WPF Dispatcher

Obtaining the Dispatcher

Executing Work Through the Dispatcher

WinRT Dispatcher

Obtaining the Dispatcher

Executing Work Through the Dispatcher

UI Timers

Windows Forms Timer

WinRT and WPF Dispatch Timers

WPF Freezable Components

Too Much of a Good Thing

Summary

images Chapter 7: async and await

Making Asynchronous Programming Simpler

What Do async and await Actually Do?

Returning Values from async Methods

Should You Always Continue on the UI Thread?

Task.Delay

Task.WhenAll

Task.WhenAll, Error Handling

Task.WhenAny

async/await Mechanics

Summary

images Chapter 8: Everything a Task

TaskCompletionSource<T>

Worked Example: Creating a Foreground Task

Unit Testing and Stubbing Asynchronous Methods

Building Task-Based Combinators

Improved WhenAny

Alternative WhenAll, WhenAllOrFail

Summary

images Chapter 9: Server-Side Async

Natural Parallelism

The Problem of I/O

ASP.NET WebForms

A Synchronous WebForms Implementation

Asynchronous Pages in WebForms 4.0

Asynchronous Pages in WebForms 4.5

ASP.NET MVC

Asynchronous MVC Processing in .NET 4.0

Asynchronous MVC Processing in .NET 4.5

ASP.NET Web API

Asynchronous Web API Operations in .NET 4.0

Asynchronous Web API Operations in .NET 4.5

Windows Communication Foundation

Asynchronous WCF Services in .NET 4.0

Asynchronous WCF Services in .NET 4.5

Summary

images Chapter 10: TPL Dataflow

The Building Blocks

Producer and Consumer Revisited

Linking Blocks

Transform Block

Transform Many Block

Linking to Multiple Targets

Shutting Down Gracefully

Propagating Completion

Error Handling

Cancellation

Glue Blocks

Buffer Block

Batch Block

Broadcast Block

Joining

Asynchronous Blocks

Summary

images Chapter 11: Parallel Programming

What Is Driving the Need for Parallelism?

Coarse- and Fine-Grained Parallelism

Task and Data-Based Parallelism

Is It Worth Trying to Parallelize Everything?

Before You Parallelize

Parallel Class

Parallel.Invoke

Parallel Loops

PLINQ

Moving from Sequential LINQ to PLINQ

Influencing and Configuring the Query

ForAll

Aggregating Results

Summary

images Chapter 12: Task Scheduling

ConcurrentExclusiveSchedulerPair

Why Write a Task Scheduler?

The TaskScheduler Abstraction

Implementing QueueTask

Implementing GetScheduledTasks

Implementing TryExecuteTaskInline

Executing Tasks

Implementing a Custom Scheduler

Creating a Basic Implementation

Adding Threads on Demand

Removing Idle Threads

Unit Testing Custom Schedulers

Controlling Execution Order with Synchronization Primitives

Adding Members to the Scheduler to Provide Insight

Deriving a Testable Class from the Scheduler

Summary

images Chapter 13: Debugging Async with Visual Studio

Types of Multithreading Bugs

Data Corruption

Race Conditions

Deadlocks

Runaway Threads

The Limitations of Using Visual Studio for Debugging

The Interactive Debugger

It Works on My Machine

Multithreaded Visual Studio Debugging Basics

Breakpoints and Threads

Locals, Autos, and Watch Windows

The Call Stack Window

The Threads Window

Debugging Tasks

The Parallel Tasks / Tasks Window

The Parallel Stacks Window

The Concurrency Visualizer

Summary

images Chapter 14: Debugging Async—Beyond Visual Studio

Memory Dumps

Generating a Memory Dump

Task Manager

DebugDiag

ADPLUS

Analyzing Memory Dumps

WinDbg

SOS

SOSEX

PSSCOR

Summary

Index

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.144.248