21
Pro Asynchronous Programming with .NET Richard Blewett Andrew Clymer

Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

  • Upload
    hadiep

  • View
    234

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

Pro Asynchronous Programming with .NET

Richard Blewett

Andrew Clymer

Page 2: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

Pro Asynchronous Programming with .NET

Copyright © 2013 by Richard Blewett and Andrew Clymer

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.

ISBN-13 (pbk): 978-1-4302-5920-6

ISBN-13 (electronic): 978-1-4302-5921-3

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

President and Publisher: Paul ManningLead Editor: Gwenan SpearingTechnical Reviewer: Liam WestleyEditorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel,

Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh

Coordinating Editor: Christine RickettsCopy Editor: James FraleighCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi GlobalCover Designer: Anna Ishchenko

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

For information on translations, please e-mail [email protected], or visit www.apress.com.

Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.

Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/.

Page 3: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

For my mum and dad

—Rich

For my loving wife

For my mum and dad

From tiny ZX81 mighty programs of today do grow.

—Andy

Page 4: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow
Page 5: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

v

Contents at a Glance

About the Authors ������������������������������������������������������������������������������������������������������������� xvii

About the Technical Reviewer ������������������������������������������������������������������������������������������� xix

Acknowledgments ������������������������������������������������������������������������������������������������������������� xxi

Chapter 1: An Introduction to Asynchronous Programming ■ ����������������������������������������������1

Chapter 2: The Evolution of the �NET Asynchronous API ■ ���������������������������������������������������7

Chapter 3: Tasks ■ ��������������������������������������������������������������������������������������������������������������31

Chapter 4: Basic Thread Safety ■ ���������������������������������������������������������������������������������������57

Chapter 5: Concurrent Data Structures ■ ���������������������������������������������������������������������������89

Chapter 6: Asynchronous UI ■ ������������������������������������������������������������������������������������������113

Chapter 7: async and await ■ �������������������������������������������������������������������������������������������133

Chapter 8: Everything a Task ■ �����������������������������������������������������������������������������������������149

Chapter 9: Server-Side Async ■ ����������������������������������������������������������������������������������������161

Chapter 10: TPL Dataflow ■ ����������������������������������������������������������������������������������������������195

Chapter 11: Parallel Programming ■ ��������������������������������������������������������������������������������233

Chapter 12: Task Scheduling ■ �����������������������������������������������������������������������������������������263

Chapter 13: Debugging Async with Visual Studio ■ ���������������������������������������������������������283

Chapter 14: Debugging Async—Beyond Visual Studio ■ ��������������������������������������������������299

Index ���������������������������������������������������������������������������������������������������������������������������������321

Page 6: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow
Page 7: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

vii

Contents

About the Authors ������������������������������������������������������������������������������������������������������������� xvii

About the Technical Reviewer ������������������������������������������������������������������������������������������� xix

Acknowledgments ������������������������������������������������������������������������������������������������������������� xxi

Chapter 1: An Introduction to Asynchronous Programming ■ ����������������������������������������������1

What Is Asynchronous Programming? ������������������������������������������������������������������������������������������1

The Drive to Asynchrony ����������������������������������������������������������������������������������������������������������������1

Mechanisms for Asynchrony ���������������������������������������������������������������������������������������������������������2

Multiple Machines ������������������������������������������������������������������������������������������������������������������������������������������������� 2

Multiple Processes ������������������������������������������������������������������������������������������������������������������������������������������������ 3

Multiple Threads ���������������������������������������������������������������������������������������������������������������������������������������������������� 3

Thread Scheduling ������������������������������������������������������������������������������������������������������������������������3

Threads and Resources �����������������������������������������������������������������������������������������������������������������4

Thread-Specific Resources ����������������������������������������������������������������������������������������������������������������������������������� 4

Resources Shared by Threads ������������������������������������������������������������������������������������������������������������������������������� 4

Summary ���������������������������������������������������������������������������������������������������������������������������������������5

Chapter 2: The Evolution of the �NET Asynchronous API ■ ���������������������������������������������������7

Asynchrony in the World of �NET 1�0 ���������������������������������������������������������������������������������������������7

System�Threading�Thread �������������������������������������������������������������������������������������������������������������������������������������� 7

Using the System Thread Pool ����������������������������������������������������������������������������������������������������������������������������� 12

Changes to Async in �NET 1�1 ������������������������������������������������������������������������������������������������������21

Page 8: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

■ Contents

viii

Asynchrony in �NET 2�0 ���������������������������������������������������������������������������������������������������������������21

Logical and Physical Separation �������������������������������������������������������������������������������������������������������������������������� 22

Passing Data into a Thread ���������������������������������������������������������������������������������������������������������������������������������� 22

Closures ��������������������������������������������������������������������������������������������������������������������������������������������������������������� 23

SynchronizationContext ��������������������������������������������������������������������������������������������������������������������������������������� 23

Event-Based Asynchronous Pattern �������������������������������������������������������������������������������������������������������������������� 25

Minor Changes in �NET 3�5 ����������������������������������������������������������������������������������������������������������26

Lambda Expressions �������������������������������������������������������������������������������������������������������������������������������������������� 27

Thread Pool Heuristics in �NET 3�5 ���������������������������������������������������������������������������������������������������������������������� 28

Big Changes in �NET 4�0 ��������������������������������������������������������������������������������������������������������������28

Remodeling the Thread Pool Queue �������������������������������������������������������������������������������������������������������������������� 28

Work-Stealing Queues ����������������������������������������������������������������������������������������������������������������������������������������� 30

Thread Pool Heuristics in �NET 4�0 ���������������������������������������������������������������������������������������������������������������������� 30

Summary �������������������������������������������������������������������������������������������������������������������������������������30

Chapter 3: Tasks ■ ��������������������������������������������������������������������������������������������������������������31

What Is a Task? ���������������������������������������������������������������������������������������������������������������������������31

Creating a Compute-Based Task �������������������������������������������������������������������������������������������������31

Passing Data into a Task �������������������������������������������������������������������������������������������������������������������������������������� 33

Dangers of Closures �������������������������������������������������������������������������������������������������������������������������������������������� 36

Returning Data from a Task ���������������������������������������������������������������������������������������������������������37

Creating I/O-Based Tasks ������������������������������������������������������������������������������������������������������������������������������������ 38

Error Handling �����������������������������������������������������������������������������������������������������������������������������41

Ignoring Errors ����������������������������������������������������������������������������������������������������������������������������������������������������� 43

Designing Task-Based APIs ��������������������������������������������������������������������������������������������������������������������������������� 45

Cancellation ��������������������������������������������������������������������������������������������������������������������������������������������������������� 46

Progress �������������������������������������������������������������������������������������������������������������������������������������������������������������� 49

Task Relationships ����������������������������������������������������������������������������������������������������������������������50

Chaining Tasks (Continuations) ���������������������������������������������������������������������������������������������������������������������������� 51

Nested and Child Tasks���������������������������������������������������������������������������������������������������������������������������������������� 53

Conclusion �����������������������������������������������������������������������������������������������������������������������������������55

Page 9: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

■ Contents

ix

Chapter 4: Basic Thread Safety ■ ���������������������������������������������������������������������������������������57

Asynchrony and Data ������������������������������������������������������������������������������������������������������������������57

It’s Not Always Good to Share ������������������������������������������������������������������������������������������������������������������������������ 57

Immutable State �������������������������������������������������������������������������������������������������������������������������������������������������� 57

Atomic State Transition ���������������������������������������������������������������������������������������������������������������������������������������� 58

Nonatomic State Transition ���������������������������������������������������������������������������������������������������������������������������������� 58

Correctness Is Not the Only Problem ������������������������������������������������������������������������������������������������������������������� 60

Thread Safety ������������������������������������������������������������������������������������������������������������������������������������������������������ 61

The Interlocked Class ������������������������������������������������������������������������������������������������������������������61

Basic Operations �������������������������������������������������������������������������������������������������������������������������������������������������� 61

Richer Functions �������������������������������������������������������������������������������������������������������������������������������������������������� 62

Monitor: The Workhorse of �NET Synchronization �����������������������������������������������������������������������64

The lock Keyword ������������������������������������������������������������������������������������������������������������������������������������������������ 67

Timing Out of Monitor Acquisition ����������������������������������������������������������������������������������������������������������������������� 68

Signaling with Monitors ��������������������������������������������������������������������������������������������������������������������������������������� 70

Signaling As a Building Block ������������������������������������������������������������������������������������������������������������������������������ 72

Optimizing for Read ���������������������������������������������������������������������������������������������������������������������73

ReaderWriterLock ������������������������������������������������������������������������������������������������������������������������������������������������ 73

ReaderWriterLockSlim ����������������������������������������������������������������������������������������������������������������������������������������� 74

A Semaphore Out of the Box �������������������������������������������������������������������������������������������������������75

Raising the Starting Gate: ManualResetEventSlim ����������������������������������������������������������������������78

CountdownEvent: Simplifying Fork and Join �������������������������������������������������������������������������������80

Barrier: Rendezvous-Based Synchronization ������������������������������������������������������������������������������81

Crossing the AppDomain Boundary with WaitHandle ������������������������������������������������������������������83

Mutex ������������������������������������������������������������������������������������������������������������������������������������������������������������������� 83

Semaphore ���������������������������������������������������������������������������������������������������������������������������������������������������������� 83

Events ������������������������������������������������������������������������������������������������������������������������������������������������������������������ 83

WaitHandle—The Kernel Synchronization Abstraction ��������������������������������������������������������������������������������������� 83

Working with Multiple WaitHandles ��������������������������������������������������������������������������������������������������������������������� 86

Integrating Standard Primitives and Kernel Objects �������������������������������������������������������������������������������������������� 87

Page 10: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

■ Contents

x

Synchronization Is Not the Only Answer �������������������������������������������������������������������������������������87

Conclusion �����������������������������������������������������������������������������������������������������������������������������������88

Chapter 5: Concurrent Data Structures ■ ���������������������������������������������������������������������������89

Simplifying Thread Safety �����������������������������������������������������������������������������������������������������������89

Lazy<T> ��������������������������������������������������������������������������������������������������������������������������������������94

Concurrent Collections ����������������������������������������������������������������������������������������������������������������98

ConcurrentDictionary<K,V> �������������������������������������������������������������������������������������������������������99

Locking Mechanics �������������������������������������������������������������������������������������������������������������������������������������������� 103

ConcurrentQueue<T> and ConcurrentStack<T> ����������������������������������������������������������������������104

ConcurrentBag<T> �������������������������������������������������������������������������������������������������������������������105

Blocking Collections ������������������������������������������������������������������������������������������������������������������106

Graceful Shutdown �������������������������������������������������������������������������������������������������������������������������������������������� 109

Consuming Enumerable ������������������������������������������������������������������������������������������������������������������������������������� 111

BlockingCollection of X �������������������������������������������������������������������������������������������������������������������������������������� 112

Summary �����������������������������������������������������������������������������������������������������������������������������������112

Chapter 6: Asynchronous UI ■ ������������������������������������������������������������������������������������������113

UI Mechanics �����������������������������������������������������������������������������������������������������������������������������113

UI Threading Model �������������������������������������������������������������������������������������������������������������������114

Synchronization Context ������������������������������������������������������������������������������������������������������������115

Send and Post ���������������������������������������������������������������������������������������������������������������������������������������������������� 116

Task Continuations �������������������������������������������������������������������������������������������������������������������������������������������� 116

Event-Based Asynchronous Pattern (EAP) ��������������������������������������������������������������������������������������������������������� 117

Background Worker ������������������������������������������������������������������������������������������������������������������������������������������� 118

Data Binding ������������������������������������������������������������������������������������������������������������������������������120

Windows Forms ������������������������������������������������������������������������������������������������������������������������������������������������� 121

Windows Presentation Foundation (WPF) ���������������������������������������������������������������������������������������������������������� 122

WinRT ���������������������������������������������������������������������������������������������������������������������������������������������������������������� 125

WPF Dispatcher �������������������������������������������������������������������������������������������������������������������������125

Obtaining the Dispatcher ����������������������������������������������������������������������������������������������������������������������������������� 125

Executing Work Through the Dispatcher ������������������������������������������������������������������������������������������������������������ 125

Page 11: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

■ Contents

xi

WinRT Dispatcher ����������������������������������������������������������������������������������������������������������������������126

Obtaining the Dispatcher ����������������������������������������������������������������������������������������������������������������������������������� 126

Executing Work Through the Dispatcher ������������������������������������������������������������������������������������������������������������ 127

UI Timers �����������������������������������������������������������������������������������������������������������������������������������127

Windows Forms Timer ��������������������������������������������������������������������������������������������������������������������������������������� 128

WinRT and WPF Dispatch Timers ����������������������������������������������������������������������������������������������������������������������� 128

WPF Freezable Components ������������������������������������������������������������������������������������������������������129

Too Much of a Good Thing ���������������������������������������������������������������������������������������������������������129

Summary �����������������������������������������������������������������������������������������������������������������������������������131

Chapter 7: async and await ■ �������������������������������������������������������������������������������������������133

Making Asynchronous Programming Simpler ���������������������������������������������������������������������������133

What Do async and await Actually Do? �������������������������������������������������������������������������������������135

Returning Values from async Methods �������������������������������������������������������������������������������������������������������������� 136

Should You Always Continue on the UI Thread? ������������������������������������������������������������������������������������������������� 138

Task�Delay ��������������������������������������������������������������������������������������������������������������������������������������������������������� 140

Task�WhenAll ����������������������������������������������������������������������������������������������������������������������������������������������������� 141

Task�WhenAll, Error Handling ���������������������������������������������������������������������������������������������������������������������������� 142

Task�WhenAny ��������������������������������������������������������������������������������������������������������������������������������������������������� 142

async/await Mechanics ������������������������������������������������������������������������������������������������������������������������������������� 144

Summary �����������������������������������������������������������������������������������������������������������������������������������148

Chapter 8: Everything a Task ■ �����������������������������������������������������������������������������������������149

TaskCompletionSource<T> �������������������������������������������������������������������������������������������������������149

Worked Example: Creating a Foreground Task ��������������������������������������������������������������������������150

Unit Testing and Stubbing Asynchronous Methods �������������������������������������������������������������������151

Building Task-Based Combinators ���������������������������������������������������������������������������������������������156

Improved WhenAny �������������������������������������������������������������������������������������������������������������������������������������������� 156

Alternative WhenAll, WhenAllOrFail ������������������������������������������������������������������������������������������������������������������� 158

Summary �����������������������������������������������������������������������������������������������������������������������������������159

Page 12: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

■ Contents

xii

Chapter 9: Server-Side Async ■ ����������������������������������������������������������������������������������������161

Natural Parallelism ��������������������������������������������������������������������������������������������������������������������161

The Problem of I/O ���������������������������������������������������������������������������������������������������������������������162

ASP�NET WebForms �������������������������������������������������������������������������������������������������������������������165

A Synchronous WebForms Implementation ������������������������������������������������������������������������������������������������������� 167

Asynchronous Pages in WebForms 4�0�������������������������������������������������������������������������������������������������������������� 169

Asynchronous Pages in WebForms 4�5�������������������������������������������������������������������������������������������������������������� 177

ASP�NET MVC �����������������������������������������������������������������������������������������������������������������������������179

Asynchronous MVC Processing in �NET 4�0 ������������������������������������������������������������������������������������������������������� 180

Asynchronous MVC Processing in �NET 4�5 ������������������������������������������������������������������������������������������������������� 184

ASP�NET Web API �����������������������������������������������������������������������������������������������������������������������185

Asynchronous Web API Operations in �NET 4�0 �������������������������������������������������������������������������������������������������� 186

Asynchronous Web API Operations in �NET 4�5 �������������������������������������������������������������������������������������������������� 187

Windows Communication Foundation ���������������������������������������������������������������������������������������188

Asynchronous WCF Services in �NET 4�0 ����������������������������������������������������������������������������������������������������������� 189

Asynchronous WCF Services in �NET 4�5 ����������������������������������������������������������������������������������������������������������� 193

Summary �����������������������������������������������������������������������������������������������������������������������������������194

Chapter 10: TPL Dataflow ■ ����������������������������������������������������������������������������������������������195

The Building Blocks �������������������������������������������������������������������������������������������������������������������195

Producer and Consumer Revisited ��������������������������������������������������������������������������������������������197

Linking Blocks ���������������������������������������������������������������������������������������������������������������������������200

Transform Block ������������������������������������������������������������������������������������������������������������������������������������������������� 201

Transform Many Block ��������������������������������������������������������������������������������������������������������������������������������������� 203

Linking to Multiple Targets �������������������������������������������������������������������������������������������������������������������������������� 206

Shutting Down Gracefully ���������������������������������������������������������������������������������������������������������213

Propagating Completion ������������������������������������������������������������������������������������������������������������������������������������ 214

Error Handling ���������������������������������������������������������������������������������������������������������������������������������������������������� 216

Cancellation ������������������������������������������������������������������������������������������������������������������������������������������������������� 218

Page 13: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

■ Contents

xiii

Glue Blocks��������������������������������������������������������������������������������������������������������������������������������220

Buffer Block ������������������������������������������������������������������������������������������������������������������������������������������������������� 220

Batch Block�������������������������������������������������������������������������������������������������������������������������������������������������������� 222

Broadcast Block ������������������������������������������������������������������������������������������������������������������������������������������������� 223

Joining ��������������������������������������������������������������������������������������������������������������������������������������������������������������� 225

Asynchronous Blocks ����������������������������������������������������������������������������������������������������������������229

Summary �����������������������������������������������������������������������������������������������������������������������������������231

Chapter 11: Parallel Programming ■ ��������������������������������������������������������������������������������233

What Is Driving the Need for Parallelism? ���������������������������������������������������������������������������������234

Coarse- and Fine-Grained Parallelism ��������������������������������������������������������������������������������������������������������������� 234

Task and Data-Based Parallelism ���������������������������������������������������������������������������������������������������������������������� 235

Is It Worth Trying to Parallelize Everything? ������������������������������������������������������������������������������������������������������ 235

Before You Parallelize ���������������������������������������������������������������������������������������������������������������������������������������� 236

Parallel Class �����������������������������������������������������������������������������������������������������������������������������237

Parallel�Invoke ��������������������������������������������������������������������������������������������������������������������������������������������������� 237

Parallel Loops ���������������������������������������������������������������������������������������������������������������������������������������������������� 239

PLINQ �����������������������������������������������������������������������������������������������������������������������������������������255

Moving from Sequential LINQ to PLINQ ������������������������������������������������������������������������������������������������������������� 256

Influencing and Configuring the Query �������������������������������������������������������������������������������������������������������������� 259

ForAll ����������������������������������������������������������������������������������������������������������������������������������������������������������������� 259

Aggregating Results ������������������������������������������������������������������������������������������������������������������������������������������ 260

Summary �����������������������������������������������������������������������������������������������������������������������������������261

Chapter 12: Task Scheduling ■ �����������������������������������������������������������������������������������������263

ConcurrentExclusiveSchedulerPair �������������������������������������������������������������������������������������������263

Why Write a Task Scheduler? ����������������������������������������������������������������������������������������������������265

The TaskScheduler Abstraction �������������������������������������������������������������������������������������������������266

Implementing QueueTask ���������������������������������������������������������������������������������������������������������������������������������� 266

Implementing GetScheduledTasks �������������������������������������������������������������������������������������������������������������������� 266

Implementing TryExecuteTaskInline ������������������������������������������������������������������������������������������������������������������ 267

Executing Tasks ������������������������������������������������������������������������������������������������������������������������������������������������� 267

Page 14: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

■ Contents

xiv

Implementing a Custom Scheduler �������������������������������������������������������������������������������������������267

Creating a Basic Implementation����������������������������������������������������������������������������������������������������������������������� 268

Adding Threads on Demand������������������������������������������������������������������������������������������������������������������������������� 270

Removing Idle Threads �������������������������������������������������������������������������������������������������������������������������������������� 272

Unit Testing Custom Schedulers ������������������������������������������������������������������������������������������������276

Controlling Execution Order with Synchronization Primitives ��������������������������������������������������������������������������� 277

Adding Members to the Scheduler to Provide Insight ��������������������������������������������������������������������������������������� 279

Deriving a Testable Class from the Scheduler ��������������������������������������������������������������������������������������������������� 280

Summary �����������������������������������������������������������������������������������������������������������������������������������281

Chapter 13: Debugging Async with Visual Studio ■ ���������������������������������������������������������283

Types of Multithreading Bugs ����������������������������������������������������������������������������������������������������283

Data Corruption ������������������������������������������������������������������������������������������������������������������������������������������������� 283

Race Conditions ������������������������������������������������������������������������������������������������������������������������������������������������� 283

Deadlocks ���������������������������������������������������������������������������������������������������������������������������������������������������������� 284

Runaway Threads ���������������������������������������������������������������������������������������������������������������������������������������������� 284

The Limitations of Using Visual Studio for Debugging ��������������������������������������������������������������284

The Interactive Debugger ���������������������������������������������������������������������������������������������������������������������������������� 285

It Works on My Machine ������������������������������������������������������������������������������������������������������������������������������������ 285

Multithreaded Visual Studio Debugging Basics �������������������������������������������������������������������������285

Breakpoints and Threads ����������������������������������������������������������������������������������������������������������������������������������� 285

Locals, Autos, and Watch Windows �������������������������������������������������������������������������������������������������������������������� 286

The Call Stack Window �������������������������������������������������������������������������������������������������������������������������������������� 286

The Threads Window ����������������������������������������������������������������������������������������������������������������������������������������� 289

Debugging Tasks �����������������������������������������������������������������������������������������������������������������������290

The Parallel Tasks / Tasks Window �������������������������������������������������������������������������������������������������������������������� 290

The Parallel Stacks Window ������������������������������������������������������������������������������������������������������������������������������ 293

The Concurrency Visualizer ������������������������������������������������������������������������������������������������������������������������������� 297

Summary �����������������������������������������������������������������������������������������������������������������������������������297

Page 15: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

■ Contents

xv

Chapter 14: Debugging Async—Beyond Visual Studio ■ ��������������������������������������������������299

Memory Dumps �������������������������������������������������������������������������������������������������������������������������299

Generating a Memory Dump �����������������������������������������������������������������������������������������������������299

Task Manager ���������������������������������������������������������������������������������������������������������������������������������������������������� 300

DebugDiag ��������������������������������������������������������������������������������������������������������������������������������������������������������� 301

ADPLUS �������������������������������������������������������������������������������������������������������������������������������������������������������������� 307

Analyzing Memory Dumps���������������������������������������������������������������������������������������������������������309

WinDbg �������������������������������������������������������������������������������������������������������������������������������������������������������������� 309

SOS �������������������������������������������������������������������������������������������������������������������������������������������������������������������� 310

SOSEX ���������������������������������������������������������������������������������������������������������������������������������������������������������������� 318

PSSCOR ������������������������������������������������������������������������������������������������������������������������������������������������������������� 319

Summary �����������������������������������������������������������������������������������������������������������������������������������319

Index ���������������������������������������������������������������������������������������������������������������������������������321

Page 16: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow
Page 17: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

xvii

About the Authors

Richard Blewett is a professional software developer and trainer living in Bristol in the UK. He has been writing software for over 20 years and has spent most of that time building distributed systems of one form or another. He first started writing multithreaded code in C on OS/2 and continued when moving to Windows, COM, and .NET. Over the years he has built software for banks, insurance companies, travel companies, emergency services, health companies, and more. He is a cofounder of Rock Solid Knowledge, a development, training and consultancy company specializing in .NET development, and a Microsoft Integration MVP.

Andrew Clymer is a professional software developer and educator living in Chippenham, Wiltshire (UK). Andy cut his teeth working in various startups and programming on a host of platforms, finally working for a small startup that was acquired by Cisco in 1997. Having worked for Cisco for a few years, the lure of the startup world was too much to resist. He is a cofounder of Rock Solid Knowledge.

Page 18: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow
Page 19: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

xix

About the Technical Reviewer

Liam Westley has been using Microsoft technologies since the days of Visual Basic 1.0 and is currently a Microsoft MVP in C#. He’s a prolific speaker in the UK developer community, talking at user groups and community conferences.

After 15 years in the television industry, including working with BSkyB, Reuters Television, chellomedia, GMTV, and the QVC UK shopping channel, Liam moved into the world of tech startups in London.

He is currently one of a team of application architects at Huddle, where he works with some of the best .NET developers and UX designers to deliver world-class collaboration software. He quite likes working just off Old Street as there is some fantastic food and coffee to be had within a few minutes’ walk.

Photo courtesy of Ian Battersby/DDDNorth.

Page 20: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow
Page 21: Pro Asynchronous Programming with - Springer978-1-4302-5921-3/1.pdf · Pro Asynchronous Programming with .NET ... Chapter 7: async and await ... Chapter 10: TPL Dataflow

xxi

Acknowledgments

We’d like to thank all the folks at Apress who have helped us ship this book: Gwenan Spearing for getting involved in the first place; Christine Ricketts for her patience and perseverance in keeping us on track; Tom Welsh and Matthew Moodie for their great editing; and James Fraleigh for his copyediting. We’d also like to thank Liam Westley for his technical reviews of the chapters.

Thank you and apologies to Michele, Carmel, Tegen, and Ewan for bearing with me as I paced around the house with writer’s block. I couldn’t have gotten here without your understanding and support.

—Richard Blewett

Thanks to Jules, Emily, and Joseph for putting up with me juggling so many things over the last 12 months and for picking up the things I dropped. Thanks to my Mum and Dad, who through the purchase of many home computers set me off on the most incredible of journeys.

—Andrew Clymer