AsyncinC#5.0.pdf带书签
最好的解说 C# Async 书籍,带书签,无水印Async in (#5.0Alex davies○ REILLYBeijing· Cambridge· Farnham·Koln· Sebastopol· TokyoAsync in (#5.0by alex daviesCopyright o 2012 Alex Davies. All rights reservedPrinted in the United States of americaPublished by O Reilly Media, Inc, 1005 Gravenstein Highway North, Sebastopol, CA 95472O'Reilly books may be purchased for educational, business, or sales promotional use. Online editionsarealsoavailableformosttitles(http://my.safaribooksonline.com).Formoreinformationcontactourcorporate/institutionalsalesdepartment800-998-9938orcorporate@oreilly.comEditor: Rachel roumeliotisCover Designer: Karen MontgomeryProduction Editor: Rachel SteelyInterior Designer: David Futatolustrators: Robert romano and Rebecca DemarestRevision history for the first Edition2012-09-07First releaseSeehttp://oreilly.com/catalog/errata.cspisbn=9781449337162forreleasedetailsNutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks ofO'Reilly Media, Inc. Async in C# 5.0, the image of a palm cockatoo, and related trade dress are trademarks of o'Reilly mMany of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and O'Reilly Media, Inc, was aware of atrademark claim, the designations have been printed in caps or initial capsWhile every precaution has been taken in the preparation of this book, the publisher and author assumeno responsibility for errors or omissions, or for damages resulting from the use of the information contained hereinISBN:978-1-449-33716-21347041364Table of contentsPreface1. IntroductiAsynchronous ProgrammingWhat's So great about Asynchronous Code?What Is asWhat async doesAsync Doesn't Solve Everything2. Why Programs Need to Be asynchronousDesktop User Interface ApplicationsAn analogy: The Cafe6Web Application Server CodeAnother Analogy: The Restaurant KitchenSilverlight, Windows Phone, and Windows 8Parallel codeAn Example103. Writing asynchronous Code manually13Some asynchronous Patterns Used in. NETThe Simplest Asynchronous PatternAn Introduction to taskThe Problem with Manual asynchrony16Converting the Example to Use Manual Asynchronous Code4. Writing Async Methods..................19Converting the Favicon Example to asyncTask and await20Async Method Return Types21Async, Method Signatures, and InterfacesThe return Statement in Async MethodsAsync methods are contagiousAsync Anonymous Delegates and Lambdas245. What await Actually Does∴…Hibernating and resuming a method25The State of the metho26Context27Where await Can't Be Used28atch and finally blocks28lock blocks9LiNQ Query Expressions29Unsafe Codeption capti30Async methods Are Synchronous Until Needed316. The Task-Based Asynchronous Pattern33What the TAP SpecifiesUsing Task for Compute-Intensive Operations34Creating a Puppet Task35Interacting with Old Asynchronous Patterns36Cold and hot tasksUp-Front Work7. Utilities for async Code39Delaying for a period of Time39Waiting for a Collection of TasksWaiting for any One task from a Collection41Creating Your Own Combinators42Cancelling Asynchronous Operations43Returning Progress During an Asynchronous Operation448. Which Thread Runs My Code?Before the first await47During the asynchronous operationSynchronization Context in Detailawait and Synchronization Context49The Lifecycle of an Async Operation49Choosing not to Use Synchronization Context51Interacting with Synchronous Code529. Exceptions in Async Code. ...............................................55Exceptions in Async Task-Returning MethodsUnobserved Exceptions57iv Table of ContentsExceptions in async void MethodsFire and forget58ggregateException and whenall58Throwing Exceptions Synchronouslyinally in async Methods10. Parallelism Using Async,,,61await and locks61Actors62USing Actors in C#63Task Parallel Library dataflow6411. Unit Testing ASync Code67The Problem with Unit Testing in Async67Writing Working Async Tests manually68Using Unit Test Framework Support6812. Async in ASP. NET ApplicationsAdvantages of Asynchronous Web Server Code69Using Async in ASP. NET MVC 469Using Async in Older Versions of ASP. NET MVC70Using async in AsP. NEt Web Forms7113. Async in WinRT applicationsWhat Is WinRt?73IAsyncAction and iasyncoperation74CancellationProgressProviding asynchronous methods in a WinRT Component7514. The async Compiler Transform-in DepthThe stub methodThe State machine struct78The move Next MethodYour code80Transforming Returns to CompletionsGet to the right Place in the methodPausing the method for the await81Resuming after the await81Completing Synchronously81Catching ExceptionsMore Complicated Code82Writing Custom Awaitable Types83Table of Contents vInteracting with the debugger8415. The performance of async Code87Measuring async Overhead87Async Versus Blocking for a Long-Running Operation88Optimizing Async Code for a Long-Running Operation90Async Versus Manual Asynchronous CodeAsync Versus Blocking Without a Long-Running Operation91Optimizing Async Code Without a Long-Running Operation91Async Performance Summary92I Table of ContentsPrefaceAsync is a powerful feature added to the C# programming language in C# 5.0. It comesat a time when performance and parallelization are becoming a major concern of software developers Used correctly, it can help to write programs with performance andparallelization properties that would have needed reams of code without it. However,what it does to your program is complex, and there are plenty of aspects to how it worksthat aren 't immediately obviousExcepting Visual Basic.NET, which added async at the same time as C#, no othermainstream programming languages offer capabilities equivalent to async Experienceand guidance in using it in real-world programs is rare. This book is the guidance frommy experience using async, as well as ideas drawn from the designers of c# and com-puter science theory. More importantly, it shows what async is, how it works, and whyyou might want to use itIntended audienceThis book is intended for people who are already confident C# programmers. Perhapsyou are looking to understand async, to choose whether to start using it. Perhaps youhave already started using async, but need to learn advanced techniques and caveatsto make best use of itHaving said that, it doesnt assume knowledge of other advanced C# features, so thebook is approachable to C# beginners, as well as programmers confident in otheranguagesC# is used in many kinds of application, and async is useful for different reasons ineach of these. For that reason, this book looks at async from both client and serverpoints of view, including chapters specifically for ASP. NET and WinRtHow to read this bookThis book is primarily designed to be read from beginning to end, as a way to learnabout async. It introduces concepts in order, helping you to understand with examplesbefore relying on that understanding. This is especially true of the first five chapters ofthe bookThe best way to learn is by doing, so I recommend that you try out code examplesyourself. For this, you'll need a C# development environment, like Microsoft VisualStudio or MonoDevelop. Take opportunities to extend the examples and work on yourown programs while reading, to understand the ideas fullyAfter reading the book, you may want to go back and use the sixth chapter onwards asa reference for advanced topics in the use of the async these chapters are organizedInto selt-contained topIcsChapters 6 and 7 focus on techniques to use in async codeChapters 8 and 9 focus on complex behaviors of asyncChapters 10 to 13 discuss situations where async is usefulChapters 14 and 15 look at how async works internallyConventions Used in this bookThe following typographical conventions are used in this bookIndicates new terms. URLs. email addresses filenames, and file extensionsConstant widthUsed for program listings, as well as within paragraphs to refer to program elementssuch as variable or function names, databases, data types, environment variablesstatements,and keywordsConstant width italicShows text that should be replaced with user-supplied values or by values deter-mined by contextt This icon signifies a tip, suggestion, or general noteUsing〔 ode ExamplesThis book is here to help you get your job done. In general, you may use the code inthis book in your programs and documentation. You do not need to contact us forpermission unless you're reproducing a significant portion of the code. For examplewriting a program that uses several chunks of code from this book does not requirepermission. Selling or distributing a CD-ROM of examples from O'Reilly books doesrequire permission. Answering a question by citing this book and quoting exampleⅶ ii Preface
下载地址
用户评论
虽然是英文版,但还是很不错的
不错的资源,无水印
真的不错,Kindle可看
英文版,很好,锻炼锻炼
不错,清晰,有书签