Pro Asynchronous Programming with .NET.pdf
(
8760 KB
)
Pobierz
www.it-ebooks.info
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
www.it-ebooks.info
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
v
www.it-ebooks.info
Chapter 1
An Introduction to Asynchronous
Programming
There are many holy grails in software development, but probably none so eagerly sought, and yet so woefully
unachieved, as making asynchronous programming simple. This isn’t because the issues are currently unknown;
rather, they are very well known, but just very hard to solve in an automated way. The goal of this book is to help you
understand why asynchronous programming is important, what issues make it hard, and how to be successful writing
asynchronous code on the .NET platform.
What Is Asynchronous Programming?
Most code that people write is synchronous. In other words, the code starts to execute, may loop, branch, pause,
and resume, but given the same inputs, its instructions are executed in a deterministic order. Synchronous code is,
in theory, straightforward to understand, as you can follow the sequence in which code will execute. It is possible
of course to write code that is obscure, that uses edge case behavior in a language, and that uses misleading names
and large dense blocks of code. But reasonably structured and well named synchronous code is normally very
approachable to someone trying to understand what it does. It is also generally straightforward to write as long as you
understand the problem domain.
The problem is that an application that executes purely synchronously may generate results too slowly or may
perform long operations that leave the program unresponsive to further input. What if we could calculate several
results concurrently or take inputs while also performing those long operations? This would solve the problems
with our synchronous code, but now we would have more than one thing happening at the same time (at least
logically if not physically). When you write systems that are designed to do more than one thing at a time, it is called
asynchronous programming.
The Drive to Asynchrony
There are a number of trends in the world of IT that have highlighted the importance of asynchrony.
First, users have become more discerning about the responsiveness of applications. In times past, when a user
clicked a button, they would be fairly forgiving if there was a slight delay before the application responded—this was
their experience with software in general and so it was, to some degree, expected. However, smartphones and tablets
have changed the way that users see software. They now expect it to respond to their actions instantaneously and
fluidly. For a developer to give the user the experience they want, they have to make sure that any operation that could
prevent the application from responding is performed asynchronously.
Second, processor technology has evolved to put multiple processing cores on a single processor package.
Machines now offer enormous processing power. However, because they have multiple cores rather than one
1
www.it-ebooks.info
Chapter 1
■
an IntroduCtIon to asynChronous programmIng
incredibly fast core, we get no benefit unless our code performs multiple, concurrent, actions that can be mapped on
to those multiple cores. Therefore, taking advantage of modern chip architecture inevitably requires asynchronous
programming.
Last, the push to move processing to the cloud means applications need to access functionality that is potentially
geographically remote. The resulting added latency can cause operations that previously might have provided
adequate performance when processed sequentially to miss performance targets. Executing two or more of these
remote operations concurrently may well bring the application back into acceptable performance. To do so, however,
requires asynchronous programming.
Mechanisms for Asynchrony
There are typically three models that we can use to introduce asynchrony: multiple machines, multiple processes,
and multiple threads. All of these have their place in complex systems, and different languages, platforms, and
technologies tend to favor a particular model.
Multiple Machines
To use multiple machines, or
nodes,
to introduce asynchrony, we need to ensure that when we request the
functionality to run remotely, we don’t do this in a way that blocks the requester. There are a number of ways
to achieve this, but commonly we pass a message to a queue, and the remote worker picks up the message and
performs the requested action. Any results of the processing need to be made available to the requester, which again
is commonly achieved via a queue. As can be seen from Figure
1-1,
the queues break blocking behavior between the
requester and worker machines and allow the worker machines to run independently of one another. Because the
worker machines rarely contend for resources, there are potentially very high levels of scalability. However, dealing
with node failure and internode synchronization becomes more complex.
Figure 1-1.
Using queues for cross-machine asynchrony
2
www.it-ebooks.info
Plik z chomika:
Phoob
Inne pliki z tego folderu:
.NET-4.0-Generics-Beginners-Guide.pdf
(7007 KB)
Accelerated c# 3.pdf
(6441 KB)
AOP in .NET.pdf
(9007 KB)
Apress - Expert C# 2008 Business Objects (2009).pdf
(24017 KB)
Apress.Visual.C.Sharp.2010.Recipes.A.Problem.Solution.Approach.Mar.2010.pdf
(9537 KB)
Inne foldery tego chomika:
ASP.NET
ASP.NET MVC
BizTalk 2010
Blend
Certification
Zgłoś jeśli
naruszono regulamin